我有一个plist文件,我想编辑这个文件,但NSDictionary中的setObject()不运行,我的代码是:
NSString *documentsDirectoryConf = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES) objectAtIndex:0];
NSString *pathConf = [documentsDirectoryConf stringByAppendingPathComponent: @"Test.plist"];
NSMutableDictionary *infoConf = [[NSMutableDictionary alloc] initWithContentsOfFile:pathConf];
NSMutableArray* defaultIm = [infoConf objectForKey:@"DatosDescargas"];
NSMutableDictionary *banners = [defaultIm objectAtIndex:0];
NSInteger numI = (int)[banners objectForKey:@"NumImages"];
NSInteger numD = (int)[banners objectForKey:@"DownloadImges"];
NSLog(@"Numero total de Imagenes: %i",numI);
NSLog(@"Numero de Imagenes descargadas: %i",numD);
**numI = 7206;
numD = 5;**
[banners setObject:(id)numI forKey:@"NumImages"];
[banners setObject:(id)numD forKey:@"DownloadImges"];
[defaultIm replaceObjectAtIndex:0 withObject:banners];
[infoConf setObject:defaultIm forKey:@"DatosDescargas"];
pathConf = [documentsDirectoryConf stringByAppendingPathComponent: @"Test.plist"];
//save content to the documents directory
[infoConf writeToFile:[pathConf stringByExpandingTildeInPath]
atomically:TRUE];
defaultIm = [infoConf objectForKey:@"DatosDescargas"];
banners = [defaultIm objectAtIndex:0];
numI = (int)[banners objectForKey:@"NumImages"];
numD = (int)[banners objectForKey:@"DownloadImges"];
NSLog(@"Numero total de Imagenes: %i",numI);
NSLog(@"Numero de Imagenes descargadas: %i",numD);
在我的控制台中,可能会看到不要编辑任何值:
2011-11-30 17:55:07.879 Catalogo-V1[5905:207] Numero total de Imagenes: 0
2011-11-30 17:55:07.880 Catalogo-V1[5905:207] Numero de Imagenes descargadas: 0
2011-11-30 17:55:07.880 Catalogo-V1[5905:207] Numero total de Imagenes: 0
2011-11-30 17:55:07.881 Catalogo-V1[5905:207] Numero de Imagenes descargadas: 0
我的文件有这种格式,必要时修改第二个字典,之后修改一个数组和第一个字典:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>DatosDescargas</key>
<array>
<<dict>
<key>NumImages</key>
<integer>0</integer>
<key>DownloadImges</key>
<integer>0</integer>
</dict>
</array>
</dict>
</plist>
答案 0 :(得分:2)
您需要将其转换为NSNumber
[NSNumber numberWithInt:numI];
然后你可以检索它:
numI = [[banners objectForKey:@"NumImages"] intValue];