views:

149

answers:

1

In the application that I am writing I am trying to write a list of fortunes for a fortune cookie out to a property list. In the simulator this works as expected and everything is great. On the iPod Touch I have it reads from said list fine but it never updates the list.

Are there any differences between updating property lists using the simulator and using the iPod Touch?

    if(indexEdit == [data count])
    {
     NSString *temp = [NSString stringWithFormat:@"%@", textField.text];
     [self.data addObject:temp];
    }
    else
    {
     NSString *temp = [NSString stringWithFormat:@"%@", textField.text];
     [data replaceObjectAtIndex:indexEdit withObject:temp];
    }
    NSString *errorDesc;
    NSString *bundlePath = [[NSBundle mainBundle] pathForResource:@"Fortunes"  ofType:@"plist"];
    NSData *plistData = [NSPropertyListSerialization dataFromPropertyList:data format:NSPropertyListXMLFormat_v1_0 errorDescription:&errorDesc];
    if (plistData)
    {
        [plistData writeToFile:bundlePath atomically:YES];
    }
    else 
    {
        NSLog(errorDesc);
        [errorDesc release];
    }
+4  A: 

I think that your application bundle is considered to be pretty much read only. If you want to write a file you should put it in the "Documents" folder, something like:

NSArray *savePaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSMutableString *savePath = [NSMutableString stringWithString:[savePaths objectAtIndex:0]];
[savePath appendString:@"/Fortunes"];

// blah blah

[plistData writeToFile:savePath atomically:YES];
Stephen Darlington
I had to say Fortunes.plist instead of Fortunes but it works. Thank you.
Bryan Hare
I'd do: NSString *filename = [savePathstringByAppendingPathComponent:@"Fortunes.plist"]; instead just so you do have to worry about slashes
Benny Wong
Good point, Benny. In my defence I wrote that code when I first started learning Cocoa!
Stephen Darlington