Reputation: 83
I'm getting a weird behavior when testing my app on a device. This is the function I'm using to update values for keys, which works smoothly on my iphone simulator. Very well indeed. However the nslog on line 6 of plist returns null on device. Do you have any ideas about why and what I am doing wrong? thank you in advance
-(void) updateData:(int)contacto campo:(int)campo {
NSString *rootPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *plistPath = [rootPath stringByAppendingPathComponent:@"Data.plist"];
NSMutableDictionary *plistDict = [[NSMutableDictionary alloc] initWithContentsOfFile:plistPath];
NSLog(@"%@",plistDict);
switch (campo) {
case 0:
veterinarioID = contacto;
[plistDict setValue:[NSNumber numberWithInt:veterinarioID] forKey:@"veterinario"];
NSLog(@"%d",veterinarioID);
break;
case 1:
peluqueriaID = contacto;
[plistDict setValue:[NSNumber numberWithInt:peluqueriaID] forKey:@"peluqueria"];
NSLog(@"%d",peluqueriaID);
break;
case 2:
residenciaID = contacto;
[plistDict setValue:[NSNumber numberWithInt:residenciaID] forKey:@"residencia"];
NSLog(@"%d",residenciaID);
break;
default:
break;
}
[plistDict writeToFile:plistPath atomically: YES];
}
Upvotes: 1
Views: 124
Reputation: 41662
My guess is that your code has evolved over time, and that you have had an existing dictionary in the simulator for a long time. Thus the file has been around for a long time there.
On the device now, if the file is not there, the dictionary is nil, and noting gets saved ever.
Change your code to the following and I believe the problem will be fixed:
NSMutableDictionary *plistDict = [[NSMutableDictionary alloc] initWithContentsOfFile:plistPath];
if(!plistDict) plistDict = [NSMutableDictionary new]; // I like using 'new'
NSLog(@"%@",plistDict);
Upvotes: 2