Iphone 通过plist获取信息
如果我有一个像这样的警察Iphone 通过plist获取信息,iphone,objective-c,nsarray,plist,Iphone,Objective C,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
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
如何将所有标题放在一个数组中,将所有描述放在另一个数组中?请参阅
这就是键值编码的魅力所在
NSArray * plistContents = [NSArray arrayWithContentsOfFile:pathToPlist];
NSArray * titles = [plistContents valueForKey:@"Title"];
NSArray * descriptions = [plistContents valueForKey:@"Description"];
这里的秘密是,对数组调用valueForKey:
会返回一个新的对象数组,其中包含对数组中的每个对象调用valueForKey:
的结果。在字典上调用valueForKey:
相当于使用objectForKey:
(如果您使用的键是键值对中的键)。有关详细信息,请参阅
一句警告:当你开始看到奇怪的结果时,使用“Description”键可能会让你毛骨悚然,因为一个拼写错误,你就会开始在每个字典上调用该方法(这不是你想要的)。Dave DeLong rocks!我差几秒钟就发帖了。令人惊叹的!
NSArray * plistContents = [NSArray arrayWithContentsOfFile:pathToPlist];
NSArray * titles = [plistContents valueForKey:@"Title"];
NSArray * descriptions = [plistContents valueForKey:@"Description"];