简体   繁体   English

基于NSStrings数组从NSArray对象中提取字符串

[英]Extracting strings from a NSArray of objects, based on a array of NSStrings

OK, this is a bit obscure, but it's giving me a headache. 好吧,这有点模糊,但它让我头疼。

If you have an array of strings 如果你有一个字符串数组

{@"1", @"2", @"4"}

And you have a array of Recipe objects 而且你有一个Recipe对象数组

{ {recipe_name:@"Lasagna", recipe_id:@"1"}
  {recipe_name:@"Burger", recipe_id:@"2"}
  {recipe_name:@"Pasta", recipe_id:@"3"}
  {recipe_name:@"Roast Chicken", recipe_id:@"4"}
  {recipe_name:@"Sauerkraut", recipe_id:@"5"}
}

How would I, using the first array, create an array like this: 我将如何使用第一个数组创建一个这样的数组:

{@"Lasagna", @"Burger", @"Roast Chicken"}

In ither words, it is taking the numbers in the first array and creating an array of recipe_names where the recipe_id matches the numbers... 用它来说,它是取第一个数组中的数字并创建一个recipe_names数组,其中recipe_id匹配数字......

Use an NSPredicate to specify the type of objects you want, then use -[NSArray filteredArrayUsingPredicate:] to select precisely those objects: 使用NSPredicate指定所需的对象类型,然后使用-[NSArray filteredArrayUsingPredicate:]精确选择这些对象:

NSArray *recipeArray = /* array of recipe objects keyed by "recipe_id" strings */;
NSArray *keyArray = /* array of string "recipe_id" keys */;
NSPredicate *pred = [NSPredicate predicateWithFormat:@"recipe_id IN %@", keyArray];
NSArray *results = [recipeArray filteredArrayUsingPredicate:pred];

NSPredicate uses its own mini-language to build a predicate from a format. NSPredicate使用自己的迷你语言从格式构建谓词。 The format grammar is documented in the "Predicate Programming Guide." 格式语法记录在“谓词编程指南”中。

If you are targeting iOS 4.0+, a more flexible alternative is to use -[NSArray indexesOfObjectsPassingTest:] : 如果您的目标是iOS 4.0+,则可以使用更灵活的替代方法-[NSArray indexesOfObjectsPassingTest:]

NSIndexSet *indexes = [recipeArray indexesOfObjectsPassingTest:
        ^BOOL (id el, NSUInteger i, BOOL *stop) {
            NSString *recipeID = [(Recipe *)el recipe_id];
            return [keyArray containsObject:recipeID];
        }];
NSArray *results = [recipeArray objectsAtIndexes:indexes];

Your array of recipe objects is basically a dictionary: 您的配方对象数组基本上是一个字典:

NSDictionary *recipeDict =
  [NSDictionary dictionaryWithObjects:[recipes valueForKey:@"recipe_name"]
                              forKeys:[recipes valueForKey:@"recipe_id"]];

And on a dictionary you can use the Key-Value Coding method: 在字典上,您可以使用键值编码方法:

NSArray *result = [[recipeDict dictionaryWithValuesForKeys:recipeIDs] allValues];

Assuming that your Recipe objects are key-value compliant (which they almost always are) you can use a predicate like so: 假设您的Recipe对象符合键值(它们几乎总是如此),您可以使用如下谓词:

NSArray *recipes= // array of Recipe objects
NSArray *recipeIDs=[NSArray arrayWithObjects:@"1",@"2",@"3",nil];
NSPredicate *pred=[NSPredicate predicateWithFormat:@"recipe_id IN %@", recipeIDs];
NSArray *filterdRecipes=[recipes filteredArrayUsingPredicate:pred];
NSArray *recipeNames=[filterdRecipes valueForKey:@"recipe_name"];

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

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