开发者

Match multiple strings in multiple NSArray

开发者 https://www.devze.com 2023-02-25 11:17 出处:网络
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

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

The XML contains stories, each story has three criteria, say 'Fruit', 'Veg', 'Spice', each containing a single phrase. 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

<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.

i.e. 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

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

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

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


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
   }
0

精彩评论

暂无评论...
验证码 换一张
取 消