0

如果我有 10 个孩子(对象),每个孩子去不同的学校(数组),并且有不同的衬衫(对象中的值)。根据衬衫颜色找到特定孩子的最佳方法是什么?

现在,我这样做,这似乎有点长:

      Match *match; 
      match.match_id = @"The match id i want to find": 

      //Check array 1 for the object
      for (Match *tempMatch in matchesGrouped)
         {
             if (tempMatch.match_id == match.match_id)
             {
                 match = tempMatch; 
                 matchFound = YES; 
                 break; 
             }
         }

      //Check array 2 for the object
      for (Match *tempMatch in matchesSingle)
         {
             if (tempMatch.match_id == match.match_id)
             {
                 match = tempMatch; 
                 matchFound = YES; 
                 break; 
             }
         }

       etc for the rest of the arrays... 

match_id 是每个匹配项的唯一整数。

提前致谢

编辑:

比赛看起来像这样:

@interface Match : UIViewController <NSCoding> 
{

}

//Match
@property (nonatomic) int match_id; 
@property (nonatomic) int matchStatus; 
@property (nonatomic) int numberOfPlayers; 
etc...
4

3 回答 3

2

使用 nspredicate,这些对象是 NSDictionarys 吗?尝试这个:

NSArray *filtered = [matchesGrouped filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"(match_id == %@)", match_id]];
于 2012-06-03T13:35:19.410 回答
1

像这样的例子怎么样:

- (IBAction)buttonArrayInArrayPressed:(id)sender 
{
    NSString *item = @"a2item2";
    NSArray *a1 = [NSArray arrayWithObjects:@"a1item1",@"a1item2",@"a1item3", nil];
    NSArray *a2 = [NSArray arrayWithObjects:@"a2item1",@"a2item2",@"a2item3", nil];
    NSArray *ax = [NSArray arrayWithObjects:a1, a2, nil];
    for (NSArray *outsideArray in ax)
    {
        for (NSString *myItem in outsideArray)
        {
            if ([myItem isEqualToString:item])
            {
                NSLog(@"Found item: %@", myItem);
                return;
            }
        }
    }
}
于 2012-06-03T16:00:41.383 回答
1

您可以基于数组 #1 中的对象创建一个 NSMutableArray,然后将其他数组中的对象添加到其中(arrayWithArray:, addObjectsFromArray:)。完成后,将有一个容器可供搜索。

(您发布的一个问题是,如果在第一个数组中找到了孩子,您会继续搜索其余的。)

于 2012-06-03T13:40:47.940 回答