2014-04-26 86 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不要忘記接受它 –