写/读plist文件iPhone [英] Write/Read plist file iPhone
问题描述
我在我的iphone应用程序中有一个plist,我想从我的单个读取和写入一个整数并形成它。
I have plist in my iphone app and I want to read and write an integer from my single to and form it.
我有这个来读它:
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];
我在这两个部分都收到错误:
I am getting an error in both parts:
无效的转换形式objc_object * to int
Invalid conversion form objc_object* to int
和
无效的转换表单int to objc_object
Invalid conversion form int to objc_object
感谢任何帮助,谢谢。
推荐答案
两件事:
When you're setting a key in a dictionary, you're probably after
setObject:forKey:
instead ofsetValue:forKey:
.
字典保存对象(类型 id
或 NSString *
或您自己的Obj-C对象),而不是原始类型(int,BOOL等)。
Dictionaries hold objects (type id
or NSString*
or your own Obj-C objects), not primitive types (int, BOOL, etc).
当你想在Obj-C集合中存储一个原始类型时,你可以将它包装在一个像这样的对象:
When you want to store a primitive type in an Obj-C collection, you can "wrap" it in an object like this:
[plistDict setObject:[NSNumber numberWithInt:score.highScore] forKey:@"score"];
然后像这样回复:
score.highScore = [[pListDict objectForKey:@"score"] intValue];
这篇关于写/读plist文件iPhone的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!