0
votes

i am trying to create a plist file, write data to it and then read data from it using the below code, but somehow the app crashes. I am trying to store a string value. Can anybody please help me out with this,

 //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..

Error that i get,

*** 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
The expression "somehow the app crashes" is not really helpful at all. You may want to try using the debugger and find out at which point exactly that app is crashing.Till
What line does it crash on? With what error?GoatInTheMachine
Sorry, I have edited my code to add the error.developer

2 Answers

2
votes

instead of

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

just use

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

this is already the NSString object .. and there is no function "stringValue" in it.

1
votes

Till is right, you didn't provide enough data for us to work with. But I'm having a good morning so I'll take a couple stabs in the dark. I would check these lines:

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

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

Step through your code till it breaks. That will show you where your error is.