写/读plist文件iPhone

Jos*_*ane 6 iphone objective-c plist

我在我的iphone应用程序中有一个plist,我想从我的单个读取和写入一个整数并形成它.

我有这个来读它:

 scoreData *score = [scoreData sharedData];

 filePath = @"stats.plist";
 NSMutableDictionary* plistDict = [[NSMutableDictionary alloc] initWithContentsOfFile:filePath];

 score.highScore = [plistDict objectForKey:@"score"];
Run Code Online (Sandbox Code Playgroud)

然后写信给它:

scoreData *score = [scoreData sharedData];

 NSMutableDictionary* plistDict = [[NSMutableDictionary alloc] initWithContentsOfFile:filePath];

 [plistDict setValue:score.highScore forKey:@"score"];
 [plistDict writeToFile:filePath atomically: YES];
Run Code Online (Sandbox Code Playgroud)

我在这两个部分都收到错误:

无效的转换形式objc_object*到int

int到objc_object的转换形式无效

我感谢任何帮助,谢谢.

Ben*_*tto 7

两件事情:

  1. 当您在字典中设置密钥时,您可能会在 setObject:forKey:而不是setValue:forKey:.

  2. 字典包含对象(类型idNSString*或您自己的Obj-C对象),而不是基本类型(int,BOOL等).

如果要在Obj-C集合中存储基本类型,可以将其"包装"在如下对象中:

[plistDict setObject:[NSNumber numberWithInt:score.highScore] forKey:@"score"];
Run Code Online (Sandbox Code Playgroud)

然后像这样回复:

score.highScore = [[pListDict objectForKey:@"score"] intValue];
Run Code Online (Sandbox Code Playgroud)