从plist文件中读取数据 [英] Reading data from a plist file

查看:95
本文介绍了从plist文件中读取数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为我的iPhone应用程序实现保存状态。



我有一个名为SaveData.plist的plist文件,我可以通过它读取它以下

  NSString * pListPath2 = [bundle pathForResource:@SaveDataofType:@plist]; 
NSDictionary * dictionary2 = [[NSDictionary alloc] initWithContentsOfFile:pListPath2];

self.SaveData = dictionary2;
[字典发布];

Plist文件有成员



SavedGame这是一个布尔值来告诉应用程序这里是否确实存在有效数据(如果他们没有在游戏中退出应用程序,我不希望他们成为还原点。



得分是一个NSNumber。
时间是一个NSNumber



Playfield是一个16元素的NSNumbers数组



如何访问NSDictionary中的这些元素?

解决方案

试试 [NSDictionary objectForKey:]



其中Key是plist中成员的名称。



例如:

  BOOL save = [[dictionary2 objectForKey:@SavedGame] boolValue] ; 

将存储在plist中的对象SavedGame存储到名为save的新BOOL中。 / p>

我想你的SavedGame布尔实际上存储在 NSDictionary 作为 NSNumber ,因此使用boolValue来获取 NSNumber 的布尔值。



试试这个文档: Apple NSDictionary Reference


I'm trying to implement a Save State for my iPhone App.

I've got a plist file called SaveData.plist and I can read it in via the following

NSString *pListPath2 = [bundle pathForResource:@"SaveData" ofType:@"plist"];
NSDictionary *dictionary2 = [[NSDictionary alloc] initWithContentsOfFile:pListPath2];

self.SaveData = dictionary2;
[dictionary release];

The Plist file has members

SavedGame which is a Boolean to tell the app if there really is valid data here (if they did not exit the app in the middle of a game, I don't want their to be a Restore Point.

Score which is an NSNumber. Time which is an NSNumber

Playfield which is a 16 element array of NSNumbers

How do I access those elements inside of the NSDictionary?

解决方案

Try [NSDictionary objectForKey:]

Where Key is the name of the member in the plist.

For example:

BOOL save = [[dictionary2 objectForKey:@"SavedGame"] boolValue];

will store the object stored in the plist for the key SavedGame into a new BOOL named save.

I imagine that your SavedGame Boolean is actually stored in the NSDictionary as an NSNumber, hence the use of boolValue to get the Boolean Value of the NSNumber.

Try this documentation: Apple NSDictionary Reference.

这篇关于从plist文件中读取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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