I have a plist that has an array of dicts. in the dict there is a KEY with the name UID. I want to query the plist where UID="1234" .. how would I search?
sample
<array>
&l开发者_如何学Ct;dict>
<key>UID</key>
<string>1234</string>
<key>name</key>
<string>bob</string>
</dict>
....
</array>
Read in the plist as an array of dictionaries and then use filteredArrayUsingPredicate:
method on NSArray
:
NSString *path = [[NSBundle mainBundle] pathForResource:@"MyInfo" ofType:@"plist"];
NSArray *plistData = [NSArray arrayWithContentsOfFile:path];
NSPredicate *filter = [NSPredicate predicateWithFormat:@"UID = %d", 1234];
NSArray *filtered = [plistData filteredArrayUsingPredicate:filter];
NSLog(@"found matches: %@", filtered);
Read in the plist as an array of dictionaries and then use the objectForKey: method of NSDictionary.
NSString *path = [[NSBundle mainBundle] pathForResource:@"MyInfo" ofType:@"plist"];
NSArray *plistData = [[NSArray arrayWithContentsOfFile:path] retain];
for (NSDictionary *dict in plistData) {
if ([[dict objectForKey:@"UID"] isEqualToString:@"1234"]) {
NSLog(@"Found it!");
}
}
精彩评论