简体   繁体   English

写/读plist文件iPhone

[英]Write/Read plist file iPhone

I have plist in my iphone app and I want to read and write an integer from my single to and form it. 我在我的iphone应用程序中有一个plist,我想从我的单个读取和写入一个整数并形成它。

I have this to read it: 我有这个来读它:

 scoreData *score = [scoreData sharedData];

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

 score.highScore = [plistDict objectForKey:@"score"];

Then to write to it: 然后写信给它:

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: 我在这两个部分都收到错误:

Invalid conversion form objc_object* to int 无效的转换形式objc_object *到int

and

Invalid conversion form int to objc_object int到objc_object的转换形式无效

I appreciate any help, thanks. 我感谢任何帮助,谢谢。

Two things: 两件事情:

  1. When you're setting a key in a dictionary, you're probably after setObject:forKey: instead of setValue:forKey: . 当你在字典中设置一个键时, 你可能在 setObject:forKey:而不是setValue:forKey:

  2. Dictionaries hold objects (type id or NSString* or your own Obj-C objects), not primitive types (int, BOOL, etc). 字典包含对象(类型idNSString*或您自己的Obj-C对象),而不是基本类型(int,BOOL等)。

When you want to store a primitive type in an Obj-C collection, you can "wrap" it in an object like this: 如果要在Obj-C集合中存储基本类型,可以将其“包装”在如下对象中:

[plistDict setObject:[NSNumber numberWithInt:score.highScore] forKey:@"score"];

And get it back like this: 然后像这样回复:

score.highScore = [[pListDict objectForKey:@"score"] intValue];

See if this answers your question: 看看这是否回答了你的问题:

How to create a new custom property list in iPhone Applications 如何在iPhone应用程序中创建新的自定义属性列表

You could use NSUserDefaults but of course highScore isn't really a default. 您可以使用NSUserDefaults,但当然highScore并不是默认值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM