2011-04-14 103 views
0

我需要通過從XML元素對字符串列表中的另一個NSArray的匹配多個字符串多的NSArray

XML包含故事的字符串匹配選擇從XML的NSArray的故事,每個故事有三個標準,說'水果','蔬菜','香料',每個包含一個單詞。樣本故事可能是這樣的:

<story> 
    <title>I love cooking</title> 
    <fruit>Oranges</fruit> 
    <veg>Cauliflower</veg> 
    <spice>Mixed spice</spice> 
    <blurb>losts of text and stuff....</blurb> 
</story> 

我有鑰匙的三個字典/一個的NSMutableDictionary值對從plist中產生

<Fruit:dictionary> 
    'Ripe bananas' : 1 
    'Green bananas' : 0 
<Veg:dictionary> 
    'Green beans' : 1 
    'Cauliflower' : 0 
<Spice:dictionary> 
    'Nutmeg' : 1 
    'Mixed spice' : 0 

我不知道鑰匙是什麼,我需要將故事中的標籤與鍵匹配。

即故事的水果標籤:「成熟的香蕉」匹配水果鍵

我可以生成鑰匙的三排列表,以「成熟的香蕉」使用

NSMutableDictionary *fruitTagsDict = [prefsDictionary objectForKey:@"Fruits"]; 
NSArray *fruitTags = [fruitTagsDict allKeys]; 

我遍歷故事XML提取一個標籤

for (id myArrayElement in storyArray) { 
    NSString *fruitString = [NSString stringWithString:[myArrayElement fruit]]; 
    //BOOL isTheObjectThere = [issueTags containsObject:fruitString]; 

    NSString *vegString = [NSString stringWithString:[myArrayElement veg]]; 

    NSString *spiceString = [NSString stringWithString:[myArrayElement spice]]; 

    //if ([[fruitTags objectAtIndex:row] isEqualToString:fruitString]) { 
    //NSLog(@"Yo %@", fruitString); 
      // ADD TO A NEW ARRAY OF MATCHING STORIES 
    //} 
     // Fails because row is undeclared 
} 

然後我開始釉。

的isTheObjectThere生產線生產零,然後在循環

末崩潰,我已經看了: Filter entire NSDictionaries out of NSArray based on multiple keys Making the Code check to see if the Text in a Text box matches any of the Strings in an NSArray

看來謂語是答案,但坦白說,我越來越糊塗。

我需要在元代碼做

repeat with stories 
    if storyFruitTag is in fruitTagArray 
    OR storyVegTag is in vegTagArray 
    OR storySpiceTag is in spiceTagArray 
     Add to new array of matching stories 

希望我已經解釋了足以讓一些指點什麼,我看着的NSMutableSet和Intersect(Xcode: Compare two NSMutableArrays),但過多信息的力量得到了我

回答

1

這裏有一個簡單的方法來判斷是否有利用關鍵路徑匹配:

if ([prefsDict valueForKeyPath:[NSString stringWithFormat:@"Fruit.%@", storyFruitTag]] || 
    [prefsDict valueForKeyPath:[NSString stringWithFormat:@"Veg.%@", storyVegTag]] || 
    [prefsDict valueForKeyPath:[NSString stringWithFormat:@"Spice.%@", storySpiceTag]]) { 
     // one of the story's tags matches a key in one of the corresponding dictionaries 
    } 
+0

作品一種享受。並將我的代碼縮小。謝謝 – JulianB 2011-04-15 01:52:39