andyleehao
andyleehao

Reputation: 2412

Get other key values when know one of them in a NSDictionary

<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>

I have this plist. Once I get the value of "BMWM6", how I know the other value in the same dictionary in the array? Could you give some help for me? Thanks in advance. It is like that: I know the BMWM6 this value, so I want to know the other key value like Engine/Dealers/Phone and so on.

Upvotes: 1

Views: 90

Answers (2)

andyleehao
andyleehao

Reputation: 2412

NSArray *carArray = [plistArray objectAtIndex:0];
    NSDictionary *resultDict;
    for (NSDictionary *dictionary in carArray) {
        NSString *userName = [dictionary objectForKey:@"UserName"];
        if ([userName isEqualToString:carName]) {
            resultDict = dictionary;
            break;
        }
    }

From harakiri, I use this and get the answer.

Upvotes: 0

cweinberger
cweinberger

Reputation: 3588

Assuming plistArray is your plist content:

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);

Upvotes: 1

Related Questions