写/读plist文件iPhone [英] Write/Read plist file iPhone

查看:122
本文介绍了写/读plist文件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

感谢任何帮助,谢谢。

推荐答案

两件事:


  1. 当您在字典中设置密钥时,之后找到你的关键字和设置值之间的区别...... setObject:forKey:而不是 setValue:forKey:

  1. When you're setting a key in a dictionary, you're probably after setObject:forKey: instead of setValue: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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆