Iphone 在多个NSArray中匹配多个字符串
我需要通过将一个XML元素中的字符串与另一个NSArray中的字符串列表相匹配来从XML的NSArray中选择故事 XML包含故事,每个故事有三个标准,比如“水果”、“蔬菜”、“香料”,每个标准都包含一个短语。示例故事可能如下所示:Iphone 在多个NSArray中匹配多个字符串,iphone,objective-c,cocoa-touch,nsarray,predicate,Iphone,Objective C,Cocoa Touch,Nsarray,Predicate,我需要通过将一个XML元素中的字符串与另一个NSArray中的字符串列表相匹配来从XML的NSArray中选择故事 XML包含故事,每个故事有三个标准,比如“水果”、“蔬菜”、“香料”,每个标准都包含一个短语。示例故事可能如下所示: <story> <title>I love cooking</title> <fruit>Oranges</fruit> <veg>Cauliflower</veg
<story>
<title>I love cooking</title>
<fruit>Oranges</fruit>
<veg>Cauliflower</veg>
<spice>Mixed spice</spice>
<blurb>losts of text and stuff....</blurb>
</story>
我在故事中循环提取一个标记
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行生成nil,然后在循环结束时崩溃
我看过:
这似乎是答案,但坦率地说,我感到困惑
在元代码中我需要做什么
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(),但是太多信息的威力让我想到了一个简单的方法来确定是否存在使用键路径的匹配:
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
}
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
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
}