简体   繁体   English

从plist文件中读取数据

[英]Reading data from a plist file

I'm trying to implement a Save State for my iPhone App. 我正在尝试为我的iPhone应用程序实现保存状态。

I've got a plist file called SaveData.plist and I can read it in via the following 我有一个名为SaveData.plist的plist文件,我可以通过以下方式读取它

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

self.SaveData = dictionary2;
[dictionary release];

The Plist file has members Plist文件有成员

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

Score which is an NSNumber. 分数是NSNumber。 Time which is an NSNumber 时间是一个NSNumber

Playfield which is a 16 element array of NSNumbers Playfield是一个16个元素的NSNumber数组

How do I access those elements inside of the NSDictionary? 如何访问NSDictionary中的这些元素?

Try [NSDictionary objectForKey:] 试试[NSDictionary objectForKey:]

Where Key is the name of the member in the plist. 其中Key是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. 将存储在plist中的对象SavedGame存储到名为save的新BOOL中。

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 . 我想你的SavedGame布尔实际上是作为NSNumber存储在NSDictionary中,因此使用boolValue来获取NSNumber的布尔值。

Try this documentation: Apple NSDictionary Reference . 试试这个文档: Apple NSDictionary Reference

For Setting Bool Value You can Use : 设置Bool值您可以使用:

[Dictionary setBool:TRUE forKey:@"preference"];

For Getting Bool value you can use : 要获得Bool值,您可以使用:

[Dictionary boolForKey:@"preference"];

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

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