2011-04-14 112 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