Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/42.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 通过plist获取信息_Iphone_Objective C_Nsarray_Plist - Fatal编程技术网

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"];