2014-04-26 88 views
0

给定的输入我真的不知道该怎么问这个,因为它是真正的痛苦要拿出一个问题的标题,但我希望你能帮助我比较NSArray的for循环与nspredicate

我有一个plist。

我读入一个NSArray

NSArray *arrayOfPlist = [[NSArray alloc] initWithContentsOfFile:path]; 

这plist中是在这种格式。

<array> 
    <dict> 
       <key>category</key> 
     <string>desert</string> 
     <key>numberOfPerson</key> 
     <string>3</string> 
     <key>recipeImage</key> 
     <string>asdasd.jpg</string> 
     <key>time</key> 
     <string>15</string> 
     <key>recipeName</key> 
     <string>asd asdad</string> 
     <key>recipeDetail</key> 
     <string>asdasd</string> 
     <key>recipeIngredients</key> 

用户将输入输入到textField中。

在那里我将它保存为SEARCHTEXT

我用NSPredicate,看是否RecipeIngredients包含它。

此时即时有麻烦

当我尝试使用这个数组

 NSArray*  haves = [recipeIngredientsOfOneRecipeString componentsSeparatedByCharactersInSet:[NSCharacterSet characterSetWithCharactersInString:@","]]; 

它给了我这个错误[<__NSCFString 0x109291950> valueForUndefinedKey:]: this class is not key value coding-compliant for the key recipeIngredients.'

另一件事是我无法想象一个适当的方式用给定的输入检查所有recipeIngredients。

我试着添加一个bool谓词每次它返回true我递增,当它的计数变得与haves.count相同我添加它,但这也不起作用。

我在为自己寻求一种解决这些问题的方式而自杀,而且我没有想法。

需要一个新的视角。

+0

你有字典的阵列来匹配用户输入的plist –

+0

@Pandey_Laxman不是阵列在哪里我认为我没有字典的数组你能指出吗? – user3570579

回答

1

您可以枚举整个plist文件使用下面的代码

NSArray* arrOfPlist = [[NSArray alloc] initWithContentsOfFile:path]; 
    NSString* strMatch;//user Input 
    for (NSDictionary* dict in arrOfPlist) 
    { 
     for (id key in dict) 
     { 
      if ([[dict objectForKey:key]isEqualToString:strMatch]) 
      { 
       //user search matches do whatever code you want here 

       break; 
      } 
     } 
    } 
+0

如果这个答案帮助你然后plz不要忘记接受它 –