问题
I'm developing an app that should search a string inside the Plist data and return a value that should prompt the user what kind of, say brand it is. For example, the user inputs "iPhone" in my text field and the app should return the brand of the product the user inputted after tapping the Go button below the text field.
Inside my Products.plist, It contains arrays of brands with certain products that brand has. Example would be:
Apple: iPhone, iPod, iPad, Mac Sony: PSP, PS3, Bravia, Xperia Samsung: Galaxy S II, Galaxy S III, Galaxy Tab
How can I do this? I have already done my app working fine but without using a plist. I just want to use a Plist for the app to be easily maintained and updated.
回答1:
Consider your plist file is called data.plist, this is how you should do this:
NSBundle *bundle = [NSBundle mainBundle];
NSString *pListpath = [bundle pathForResource:@"data" ofType:@"plist"];
NSDictionary *dictionary = [[NSDictionary alloc] initWithContentsOfFile:pListpath];
for (NSString* key in [dictionary allKeys]){
NSArray *array = [dictionary objectForKey:key];
for (int j = 0; j < [array count]; j++) {
if ([[array objectAtIndex:j] isEqualToString:@"iPhone"]) {
NSLog(@"%@",key);
}
}
}
来源:https://stackoverflow.com/questions/12292876/search-on-arrays-inside-plist-in-xcode