can*_*boy 3 iphone objective-c plist nsarray
如果我有这样的plist设置
Key Type Value
Root Array
Item 0 Dictionary
-Title String Part One
-Description String Welcome to part one. Have fun
Item 1 Dictionary
-Title String Part Two
-Description String Welcome to part two. Fun too.
Item 2 Dictionary
-Title String Part Three
-Description String Welcome to part three. It's free
Item 3 Dictionary
-Title String Part Four
-Description String It's part four. No more
Run Code Online (Sandbox Code Playgroud)
我将如何通过将所有标题放在一个数组中,并将所有描述放入另一个数组中?
Oooooooo这就是Key-Value Coding令人敬畏的地方.
NSArray * plistContents = [NSArray arrayWithContentsOfFile:pathToPlist];
NSArray * titles = [plistContents valueForKey:@"Title"];
NSArray * descriptions = [plistContents valueForKey:@"Description"];
Run Code Online (Sandbox Code Playgroud)
这里的秘诀是,valueForKey:在数组上调用会返回一个新的对象数组,其中包含对数组中valueForKey:每个事物进行调用的结果.调用valueForKey:字典可以等效于使用objectForKey:(如果您使用的密钥是键值对中的密钥).有关详细信息,请参阅文档.
需要注意的一点是:当你开始看到奇怪的结果时,使用"描述"键可能会让你撕掉一些头发,因为一个拼写错误,你实际上会开始-description在每个字典上调用这个方法(这不是你想要的).
| 归档时间: |
|
| 查看次数: |
436 次 |
| 最近记录: |