问题
This code works on simulator but not on device. I am positive this has something to do with writing to file:
NSString *path = [[NSBundle mainBundle] pathForResource:@"SongData" ofType:@"plist"];
NSArray *tempData = [[NSArray alloc] initWithContentsOfFile: path];
NSMutableArray *arr = [[NSMutableArray alloc] init];
for (NSDictionary *dict in tempData) {
NSMutableDictionary *new = [[dict mutableCopy] autorelease];
if ([[new objectForKey:@"Song Title"] isEqualToString:[[tableData objectAtIndex:[indexPath row]] objectForKey:@"Song Title"]]) {
BOOL fav = [[new objectForKey:@"Favorite"] boolValue];
[new setObject:[NSNumber numberWithBool:!fav] forKey:@"Favorite"];
}
[arr addObject:new];
}
[arr writeToFile:path atomically:YES];
I am basically writing to file the NSMutableArray, I am not sure what atomically means, but I have tried both yes and no, both don't work.
Thanks.
回答1:
You cannot write into your app bundle, because of sandbox restrictions (it would also break your code signature). You should write to your app's documents directory instead.
You can get your Documents directory with:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docPath = [paths objectAtIndex:0];
来源:https://stackoverflow.com/questions/6005544/writetofile-working-in-sim-but-not-on-device