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

配列#1のオブジェクトに基づいてNSMutableArrayを作成し、他の配列のオブジェクトをそれに追加することができます(arrayWithArray:addObjectsFromArray:)。それが完了すると、検索するコンテナは1つになります。

(投稿した内容の問題は、子が最初の配列で見つかった場合、残りの配列を検索し続けることです。)

于 2012-06-03T13:40:47.940 に答える
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 に答える