帮助plist文件

时间:2011-03-30 15:03:24

标签: iphone ios4 plist

我正在尝试创建一个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..

我得到错误,

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSCFString stringValue]: unrecognized selector sent to instance 0x4b25af0'
*** Call stack at first throw:
(
 0   CoreFoundation                      0x00dd9be9 __exceptionPreprocess + 185
 1   libobjc.A.dylib                     0x00f2e5c2 objc_exception_throw + 47
 2   CoreFoundation                      0x00ddb6fb -[NSObject(NSObject) doesNotRecognizeSelector:] + 187
 3   CoreFoundation                      0x00d4b366 ___forwarding___ + 966
 4   CoreFoundation                      0x00d4af22 _CF_forwarding_prep_0 + 50
 5   Gavel Edge                          0x00003e1e -[JobViewController tableView:didSelectRowAtIndexPath:] + 832
 6   UIKit                               0x00358794 -[UITableView _selectRowAtIndexPath:animated:scrollPosition:notifyDelegate:] + 1140
 7   UIKit                               0x0034ed50 -[UITableView _userSelectRowAtPendingSelectionIndexPath:] + 219
 8   Foundation                          0x000617f6 __NSFireDelayedPerform + 441
 9   CoreFoundation                      0x00dbafe3 __CFRUNLOOP_IS_CALLING_OUT_TO_A_TIMER_CALLBACK_FUNCTION__ + 19
 10  CoreFoundation                      0x00dbc594 __CFRunLoopDoTimer + 1220
 11  CoreFoundation                      0x00d18cc9 __CFRunLoopRun + 1817
 12  CoreFoundation                      0x00d18240 CFRunLoopRunSpecific + 208
 13  CoreFoundation                      0x00d18161 CFRunLoopRunInMode + 97
 14  GraphicsServices                    0x0170e268 GSEventRunModal + 217
 15  GraphicsServices                    0x0170e32d GSEventRun + 115
 16  UIKit                               0x002f142e UIApplicationMain + 1160
 17  Gavel Edge                          0x00001de4 main + 102
 18  Gavel Edge                          0x00001d75 start + 53
 19  ???                                 0x00000001 0x0 + 1
 )
terminate called after throwing an instance of 'NSException'

2 个答案:

答案 0 :(得分:2)

而不是

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

只需使用

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

这已经是NSString对象..并且其中没有函数“stringValue”。

答案 1 :(得分:1)

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

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

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

逐步执行代码,直至其中断。这将显示您的错误所在。