objective-c 从 plist 文件中读取数据

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1067167/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 21:49:32  来源:igfitidea点击:

Reading data from a plist file

iphoneobjective-c

提问by K2Digital

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. Time which is an NSNumber

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

Playfield which is a 16 element array of NSNumbers

Playfield 是一个 16 元素的 NSNumbers 数组

How do I access those elements inside of the NSDictionary?

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

回答by Jeff Hellman

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.

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

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

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

Try this documentation: Apple NSDictionary Reference.

试试这个文档: Apple NSDictionary Reference

回答by Shahid Aslam

For Setting Bool Value You can Use :

要设置布尔值,您可以使用:

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

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

For Getting Bool value you can use :

对于获取布尔值,您可以使用:

[Dictionary boolForKey:@"preference"];

[Dictionary boolForKey:@"preference"];