【问题标题】:Help with plist file帮助 plist 文件
【发布时间】:2011-03-30 15:03:24
【问题描述】:

我正在尝试创建一个 plist 文件,向其中写入数据,然后使用以下代码从中读取数据,但不知何故应用程序崩溃了。我正在尝试存储一个字符串值。谁能帮我解决这个问题,

 //PLIST FILE START..
    NSError *error;
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); //1
    NSString *documentsDirectory = [paths objectAtIndex:0]; //2
    NSString *path = [documentsDirectory stringByAppendingPathComponent:@"data.plist"]; //3

    NSFileManager *fileManager = [NSFileManager defaultManager];

    if (![fileManager fileExistsAtPath: path]) //4
    {
        NSString *bundle = [[NSBundle mainBundle] pathForResource:@"data" ofType:@"plist"]; //5

        [fileManager copyItemAtPath:bundle toPath: path error:&error]; //6
    }

    //And write data:

    NSMutableDictionary *data = [[NSMutableDictionary alloc] initWithContentsOfFile: path];

    //here add elements to data file and write data to file
    //NSString *value = rowValue;

    [data setObject:[NSString stringWithFormat:@"%@", rowValue] forKey:@"value"];

    [data writeToFile: path atomically:YES];
    [data release]; 

    //Read Data..
    NSMutableDictionary *savedStock = [[NSMutableDictionary alloc] initWithContentsOfFile: path];

    //load from savedStock example int value
    NSString *test;
    test = [savedStock objectForKey:@"value"];

    [savedStock release];

    NSLog(@"PLIST VALUE:%@",test);
    //PLIST FILE END..

我得到的错误,

*** 由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“-[NSCFString stringValue]:无法识别的选择器发送到实例 0x4b25af0” *** 第一次抛出调用堆栈: ( 0 核心基础 0x00dd9be9 __exceptionPreprocess + 185 1 libobjc.A.dylib 0x00f2e5c2 objc_exception_throw + 47 2核心基础0x00ddb6fb-[NSObject(NSObject)不识别选择器:]+187 3 核心基础 0x00d4b366 ___forwarding___ + 966 4 核心基础 0x00d4af22 _CF_forwarding_prep_0 + 50 5 木槌边缘 0x00003e1e -[JobViewController tableView:didSelectRowAtIndexPath:] + 832 6 UIKit 0x00358794-[UITableView _selectRowAtIndexPath:animated:scrollPosition:notifyDelegate:] + 1140 7 UIKit 0x0034ed50-[UITableView _userSelectRowAtPendingSelectionIndexPath:] + 219 8 基础 0x000617f6 __NSFireDelayedPerform + 441 9 核心基础 0x00dbafe3 __CFRUNLOOP_IS_CALLING_OUT_TO_A_TIMER_CALLBACK_FUNCTION__ + 19 10 核心基础 0x00dbc594 __CFRunLoopDoTimer + 1220 11 核心基础 0x00d18cc9 __CFRunLoopRun + 1817 12 核心基础 0x00d18240 CFRunLoopRunSpecific + 208 13 核心基础 0x00d18161 CFRunLoopRunInMode + 97 14 图形服务 0x0170e268 GSEventRunModal + 217 15 图形服务 0x0170e32d GSEventRun + 115 16 UIKit 0x002f142e UIApplicationMain + 1160 17 木槌边缘 0x00001de4 主 + 102 18 木槌边缘 0x00001d75 开始 + 53 19 ??? 0x00000001 0x0 + 1 ) 抛出 'NSException' 的实例后调用终止

【问题讨论】:

  • “应用程序以某种方式崩溃”的表述根本没有真正的帮助。您可能想尝试使用调试器并找出该应用程序究竟在什么时候崩溃了。
  • 它在哪一行崩溃?有什么错误?
  • 对不起,我已经编辑了我的代码以添加错误。

标签: iphone ios4 plist


【解决方案1】:

而不是

test = [[savedStock objectForKey:@"value"] stringValue];

随便用

test = [savedStock objectForKey:@"value"];

这已经是 NSString 对象了.. 里面没有函数“stringValue”。

【讨论】:

  • 非常感谢@Bastian。我这样做了,现在它不会给我任何错误,但它也不会打印 NSLog(@"PLIST VALUE:%@",test);还有什么我需要做的吗..
  • 在加载后尝试使用 NSLog(@"dict %@", savedStock) 打印整个字典...这样有效吗?
  • 是的,这是有效的。我得到以下输出, dict { stringVal = test;值 = TST1101; }
  • 嘿,我做了类似 NSString *test = [savedStock objectForKey:@"value"];现在它可以工作了。非常感谢你的帮助..
【解决方案2】:

直到是对的,您没有提供足够的数据供我们使用。但我有一个美好的早晨,所以我会在黑暗中刺几下。我会检查这些行:

// Maybe a path wasn't found?
NSString *documentsDirectory = [paths objectAtIndex:0];

// Maybe the resource wasn't found?
[fileManager copyItemAtPath:bundle toPath: path error:&error];

单步执行您的代码,直到它中断。这会告诉你你的错误在哪里。

【讨论】:

  • 因为我是新手,所以我使用了ipgames.wordpress.com/tutorials/writeread-data-to-plist-file url 提供的代码。如代码中所述,我在我的项目中创建了一个 plist 文件。如果我做错了什么,请告诉我,非常感谢您的帮助..
  • 无法识别的选择器意味着您的代码行的第一部分无效。看这一行: [[savedStock objectForKey:@"value"] stringValue]; [savedStock objectForKey:@"value"]
猜你喜欢
  • 2011-05-20
  • 1970-01-01
  • 2011-04-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-09-23
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多