Iphone 将NSMutableDictionary保存到plist发行版
在设备上测试我的应用程序时,我有一种奇怪的行为。这是我用来更新按键值的函数,在我的iphone模拟器上可以顺利运行。非常好。但是,plist第6行的nslog在设备上返回null。你知道我为什么做错了什么吗?先谢谢你Iphone 将NSMutableDictionary保存到plist发行版,iphone,ios,plist,nsmutabledictionary,ios5.1,Iphone,Ios,Plist,Nsmutabledictionary,Ios5.1,在设备上测试我的应用程序时,我有一种奇怪的行为。这是我用来更新按键值的函数,在我的iphone模拟器上可以顺利运行。非常好。但是,plist第6行的nslog在设备上返回null。你知道我为什么做错了什么吗?先谢谢你 -(void) updateData:(int)contacto campo:(int)campo { NSString *rootPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUse
-(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];
}
我的猜测是,您的代码随着时间的推移而演变,并且您在模拟器中已有一个字典已经很长时间了。因此,该文件已经存在了很长一段时间 在现在的设备上,如果文件不在那里,字典将为零,并且不会保存任何内容 将代码更改为以下内容,我相信问题会得到解决:
NSMutableDictionary *plistDict = [[NSMutableDictionary alloc] initWithContentsOfFile:plistPath];
if(!plistDict) plistDict = [NSMutableDictionary new]; // I like using 'new'
NSLog(@"%@",plistDict);
将plistPath放置到控制台使设备上实际存在位于
plistPath的文件?这是plistPath的控制台输出:2012-09-22 15:14:05.710 testAPP[6500:12503]/Users/Samuel/Library/Application Support/iPhone Simulator/5.1/Applications/FC9C7FC7-BAFB-4EBC-95FC-D9D256E58EA5/Documents/Data.plistPath设备plistPath的支架控制台:2012-09-22 15:16:38.142 testAPP[4922:707]/var/mobile/Applications/EEB375D7-E39B-4456-9F19-29AEC9401A79/Documents/Data.plistDavid,你的“新”方法成功了。谢谢你的帮助;)