1
<array>
<array>
    <dict>
        <key>UserName</key>
        <string>BMWM6</string>
        <key>Engine</key>
        <string>4.0</string>
        <key>Image</key>
        <string>bmwM6.png</string>
        <key>Dealers</key>
        <string>USA</string>
        <key>Phone</key>
        <string>10000</string>
        <key>Price</key>
        <string>68</string>
        <key>Category</key>
        <string>BMW</string>
    </dict>
    <dict>
        <key>UserName</key>
        <string>BMWi740</string>
        <key>Engine</key>
        <string>4.0</string>
        <key>Image</key>
        <string>bmwM6.png</string>
        <key>Dealers</key>
        <string>NewYork</string>
        <key>Phone</key>
        <string>13800138000</string>
        <key>Price</key>
        <string>235</string>
        <key>Category</key>
        <string>BMW</string>
    </dict>
    <dict>
        <key>UserName</key>
        <string>BMWX6</string>
        <key>Engine</key>
        <string>3.0</string>
        <key>Image</key>
        <string>bmwM6.png</string>
        <key>Dealers</key>
        <string>NewYork</string>
        <key>Phone</key>
        <string>13800138000</string>
        <key>Price</key>
        <string>231</string>
        <key>Category</key>
        <string>BMW</string>
    </dict>
</array>
<array>
    <dict>
        <key>UserName</key>
        <string>E60</string>
        <key>Engine</key>
        <string>3.5</string>
        <key>Image</key>
        <string>BenzE60.png</string>
        <key>Dealers</key>
        <string>NewYork</string>
        <key>Phone</key>
        <string>13800138000</string>
        <key>Price</key>
        <string>180</string>
        <key>Category</key>
        <string>BENZ</string>
    </dict>
</array>
<array>
    <dict>
        <key>UserName</key>
        <string>R8</string>
        <key>Engine</key>
        <string>4.5</string>
        <key>Image</key>
        <string>audiR8.png</string>
        <key>Dealers</key>
        <string>NewYork</string>
        <key>Phone</key>
        <string>13800138000</string>
        <key>Price</key>
        <string>220</string>
        <key>Category</key>
        <string>AUDI</string>
    </dict>
</array>

我有这个列表。一旦我得到“BMWM6”的值,我如何知道数组中同一个字典中的另一个值?你能给我一些帮助吗?提前致谢。就是这样:我知道 BMWM6 这个值,所以我想知道其他关键值,如 Engine/Dealers/Phone 等。

4

2 回答 2

1

假设 plistArray 是您的 plist 内容:

NSString *username = @"BMWM6";
NSPredicate *pre = [NSPredicate predicateWithFormat:@"UserName = %@", username];
NSDictionary *resultDict = [[plistArray filteredArrayUsingPredicate:pre] lastObject];

// now you have the dictionary
// dealer:
NSString *dealers = [resultDict objectForKey:@"Dealers"];
NSLog(@"dealers: %@", dealers);
于 2012-07-12T10:07:14.657 回答
0
NSArray *carArray = [plistArray objectAtIndex:0];
    NSDictionary *resultDict;
    for (NSDictionary *dictionary in carArray) {
        NSString *userName = [dictionary objectForKey:@"UserName"];
        if ([userName isEqualToString:carName]) {
            resultDict = dictionary;
            break;
        }
    }

从harakiri,我使用它并得到答案。

于 2012-07-13T08:50:51.737 回答