Search on Arrays inside Plist in Xcode

余生颓废 提交于 2019-12-08 10:20:24

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!