Rahul
Rahul

Reputation: 961

how we copy plist in NSDefault in ios 5

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

Answers (1)

Robert
Robert

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.

  • cmd-shift-y bring up the console, see what it says.
  • check that Bookmark.plist exists on the bundle, check if it exists in the documents directory. Check both the simulator and the device.
  • where does the execution crash?
  • set some logs e.g. if (error) NSLog(@"Error:%@",error); Log the paths that are generated.
  • does if ( ![fileManager fileExistsAtPath:myPlistPath] ) evaluate to true or false. set up another log.
  • profile the run using the zombies tool.

Do a bit more work first next time you are stuck.

Upvotes: 1

Related Questions