简体   繁体   English

匹配多个NSArray中的多个字符串

[英]Match multiple strings in multiple NSArray

I need to select stories from a NSArray of XML by matching a string from an XML element against a list of strings in another NSArray 我需要通过将XML元素中的字符串与另一个NSArray中的字符串列表进行匹配来从XML的NSArray中选择故事

The XML contains stories, each story has three criteria, say 'Fruit', 'Veg', 'Spice', each containing a single phrase. XML包含故事,每个故事都有三个条件,例如“水果”,“蔬菜”,“香料”,每个条件都包含一个短语。 A sample story might look like: 一个示例故事可能看起来像:

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

I have three dictionaries of key/value pairs in a NSMutableDictionary generated from a pList 我从pList生成的NSMutableDictionary中有三个键/值对字典

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

I don't know what the keys will be, and I need to match the tag in the story against the keys. 我不知道键是什么,我需要将故事中的标签与键进行匹配。

ie story fruit tag:'Ripe bananas' MATCHES 'Ripe bananas' in list of fruit keys 即故事水果标签:水果键列表中的“成熟香蕉”与“成熟香蕉”匹配

I can build three arrays of the keys using 我可以使用构建三个键数组

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

I loop through the story XML extracting a tag 我遍历故事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
}

Then I start to glaze out. 然后我开始上釉。

The isTheObjectThere line produces nil then crashes at end of loop isTheObjectThere行产生nil,然后在循环结束时崩溃

I've looked at: 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 我看过: 根据多个键从NSArray筛选整个NSDictionaries进行 代码检查,以查看“文本”框中的文本是否与NSArray中的任何字符串匹配

It seems predicate is the answer but frankly I getting confused. 似乎谓语是答案,但坦率地说,我感到困惑。

What I need to do in metacode 我需要在元代码中做什么

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

Hopefully I've explained enough to get some pointers, I looked into NSMutableSet and Intersect ( Xcode: Compare two NSMutableArrays ) but the power of too much information got to me 希望我已经解释了足够的指针,我研究了NSMutableSet和Intersect( Xcode:比较两个NSMutableArrays ),但是掌握了太多信息的力量

Here's a simple way to determine whether there are matches using key paths: 这是使用键路径确定是否存在匹配项的简单方法:

 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
   }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM