我需要通过将XML元素中的字符串与另一个NSArray中的字符串列表进行匹配来从NSArray中选择故事
XML包含故事,每个故事都有三个标准,比如'Fruit','Veg','Spice',每个都包含一个短语。示例故事可能如下所示:
<story>
<title>I love cooking</title>
<fruit>Oranges</fruit>
<veg>Cauliflower</veg>
<spice>Mixed spice</spice>
<blurb>losts of text and stuff....</blurb>
</story>
我在从pList生成的NSMutableDictionary中有三个键/值对词典
<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行产生nil然后在循环结束时崩溃
我看过: 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),但是有太多信息的力量来找我
答案 0 :(得分: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
}