Reputation: 961
How do I copy the plist in nsdefault in iOS 5
Here is my code:
NSError *error = nil;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *myPlistPath = [documentsDirectory stringByAppendingPathComponent:
[NSString stringWithFormat: @"%@.plist", @"Bookmark"] ];
// If it's not there, copy it from the bundle
NSFileManager *fileManager = [NSFileManager defaultManager];
if ( ![fileManager fileExistsAtPath:myPlistPath] ) {
NSString *pathToSettingsInBundle = [[NSBundle mainBundle]
pathForResource:@"Bookmark" ofType:@"plist"];
[fileManager copyItemAtPath:pathToSettingsInBundle toPath:myPlistPath error:&error];
}
[myPlistPath retain];
[myPlistPath release];
It runs in simulator correctly, but on the device it crashes.
Upvotes: 0
Views: 312
Reputation: 38213
Firstly, NSDefault is not a thing, the code provided does not even use NSUserDefaults
if thats what you meant,
Now the lines:
[myPlistPath retain];
[myPlistPath release];
are completely redundant, get rid of them.
Now use some common sense when debugging.
Bookmark.plist
exists on the bundle, check if it exists in the documents directory. Check both the simulator and the device.if (error) NSLog(@"Error:%@",error);
Log the paths that are generated.if ( ![fileManager fileExistsAtPath:myPlistPath] )
evaluate to true or false. set up another log.Do a bit more work first next time you are stuck.
Upvotes: 1