我的 iPhone 应用程序中有 plist,我想从我的 single 中读取和写入一个整数并形成它。
我有这个来阅读:
scoreData *score = [scoreData sharedData];
filePath = @"stats.plist";
NSMutableDictionary* plistDict = [[NSMutableDictionary alloc] initWithContentsOfFile:filePath];
score.highScore = [plistDict objectForKey:@"score"];
然后写入:
scoreData *score = [scoreData sharedData];
NSMutableDictionary* plistDict = [[NSMutableDictionary alloc] initWithContentsOfFile:filePath];
[plistDict setValue:score.highScore forKey:@"score"];
[plistDict writeToFile:filePath atomically: YES];
我在两个部分都遇到错误:
objc_object* 到 int 的转换无效
and
从 int 到 objc_object 的转换无效
我很感激任何帮助,谢谢。
两件事情:
当你在字典中设置键时,你可能正在追求 https://stackoverflow.com/questions/1249634/wheres-the-difference-between-setobjectforkey-and-setvalueforkey-in-nsmutabl setObject:forKey:
代替setValue:forKey:
.
字典保存对象(类型id
or NSString*
或您自己的 Obj-C 对象),而不是原始类型(int、BOOL 等)。
当你想在 Obj-C 集合中存储原始类型时,你可以将它“包装”在一个对象中,如下所示:
[plistDict setObject:[NSNumber numberWithInt:score.highScore] forKey:@"score"];
并像这样取回它:
score.highScore = [[pListDict objectForKey:@"score"] intValue];
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)