获取NSDictionary的字符串

时间:2011-02-03 21:31:43

标签: iphone xcode ipad

我有一个具有以下结构的plist:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<array>
    <dict>
        <key>height</key>
        <real>2075</real>
        <key>name</key>
        <string>Dublin-Pony-kids-25_thumb</string>
        <key>width</key>
        <real>1558</real>
    </dict>
    <dict>
        <key>height</key>
        <real>2219</real>
        <key>name</key>
        <string>Dublin-Pony-kids-26_thumb</string>
        <key>width</key>
        <real>1751</real>
    </dict>
    <dict>
        <key>height</key>
        <real>2532</real>
        <key>name</key>
        <string>Dublin-Pony-kids-27_thumb</string>
        <key>width</key>
        <real>1732</real>
    </dict>
    <dict>
        <key>height</key>
        <real>2430</real>
        <key>name</key>
        <string>Dublin-Pony-kids-28_thumb</string>
        <key>width</key>
        <real>1730</real>
    </dict>
</array>
</plist>

我想知道如何获得带有key-&gt; name的所有结果的NSString。 我想要像Dublin-Pony-kids-25_thumb,Dublin-Pony-kids-26_thumb,Dublin-Pony-kids-27_thumb,Dublin-Pony-kids-28_thumb这样的东西。

我可以像(NSString *)imageNameAtIndex:(NSUInteger)index {一样阅读 但这只返回一个值。我希望key-&gt; name的所有值都为NSString

我再次向你提供帮助。

2 个答案:

答案 0 :(得分:2)

如果你将plist加载到一个数组中(通过+[NSArray arrayWithContentsOfFile:...]),那么你应该可以这样做:

NSArray *plistContents = [NSArray arrayWithContentsOfFile:...]; //load the plist into memory
NSArray *names = [plistContents valueForKey:@"name"];

答案 1 :(得分:0)

谢谢你的回答。它给了我正确的方法。我这样称呼我的大拇指。

- (NSArray *)thumbNames {
//For the thumbs we need a different array than for the images
//Load the plist
NSString* plistPath = [[NSBundle mainBundle] pathForResource:@"ImageData" ofType:@"plist"];
NSArray *plistContents = [NSArray arrayWithContentsOfFile:plistPath]; //load the plist into memory
NSArray *thumbNames = [plistContents valueForKey:@"name"];


return thumbNames;

}

我可以将var thumbNames加载到新视图中。

干杯

德克