2012-06-03 3 views
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... 
+0

배열에 10 명의 자녀가 있습니까? 셔츠 나 셔츠 색깔이 아이들 물건의 속성입니까? 자녀 개체의 코드를 보는 것이 도움이 될 것입니다. – rdelmar

답변

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; 
      } 
     } 
    } 
} 
2

사용 자 NSPredicate, 그 객체 NSDictionarys입니까? 이 시도 : 당신은 배열 # 1에있는 개체를 기반으로있는 NSMutableArray를 생성 한 후 (arrayWithArray:, addObjectsFromArray:)에 다른 배열에서 객체를 추가 할 수

NSArray *filtered = [matchesGrouped filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"(match_id == %@)", match_id]]; 
+0

위 질문을 업데이트했습니다. 개체는 사용자 지정 클래스입니다. – BlackMouse

1

. 그 작업이 완료되면 검색 할 단일 컨테이너가 있습니다.

(당신이 게시 한 내용의 문제점은 아이가 첫 번째 배열에있는 경우, 당신이 그들의 나머지 부분을 찾고 유지하는 것입니다.)

관련 문제