Writing into plists in iOS 5.0.1

2020-04-20 21:32发布

问题:

I am using the following code to write into a plist:

  NSMutableArray *d = [[NSMutableArray alloc] initWithContentsOfFile:
                     [[NSBundle mainBundle] pathForResource:@"FavoritesTaxi" ofType:@"plist"]];
[d addObject:self.dataItem];


// write dictionary to plist
[d writeToFile:[[NSBundle mainBundle] pathForResource:@"FavoritesTaxi" ofType:@"plist"] atomically:YES];
[d release];

I tested this code on 4 different devices( 2 simulators and 2 real iphones). It worked on the following : iPhone 4.3 simulator iPhone 5.0 simulator iPhone 4.3 Real Device

But it didn't work on the following : iPhone 5.0.1 Real Device.

I really do not know why this is happening to this device. Any ideas why? And what is the solution to it?

回答1:

You can't modify the main bundle on the device, you don't have the privileges to do that. So you can't write a plist into the main bundle. I doubt this worked on the 4.3 device, it just may not have given you an error.

You should save data into the documents directory, there are tons of code samples on here for getting a path to the documents directory.