2016-09-28 5 views
1

안녕하세요, 두 개의 nsarrays 있습니다.서로 다른 값을 갖는 두 개의 nsarrays를 비교하는 방법은 무엇입니까?

어레이 A :

arrProductSelection = [[NSArray alloc]initWithObjects:@"English",@"German",@"Russian",@"Chinese",@"Spanish",@"French",@"French",@"French",@"French",@"French",@"French",@"French",@"French",nil]; 

배열 B :

I는 두 배열을 비교 선택으로 배열 B 값을 갖는 비교함으로써 어레이 A로부터 값을 취득 할 필요
arrProductSelectionB = [[NSArray alloc]initWithObjects:@"deselcted",@"selected",@"selected",@"selected",@"deselcted",@"deselcted",@"deselcted",@"deselcted",@"deselcted",@"deselcted",@"deselcted",@"deselcted",@"deselcted",nil]; 

. 그것은 독일어, 중국어, 러시아어를 nsstring으로 쉼표로 구분해야합니다.

+0

두 배열 모두 동일합니까? –

+0

이 목적으로 사전을 사용해야합니다. – Janmenjaya

+0

예 그들은 동일합니다 –

답변

3

이 시도 : 당신이 다음 easly 관리 배열의 유형을 만들 수 있다면

NSMutableArray *arrSelected = [[NSMutableArray alloc] init]; 

NSArray *arrProductSelection = [[NSArray alloc]initWithObjects:@"English",@"German",@"Russian",@"Chinese",@"Spanish",@"French",@"French",@"French",@"French",@"French",@"French",@"French",@"French",nil]; 

NSArray *arrProductSelectionB = [[NSArray alloc]initWithObjects:@"deselcted",@"selected",@"selected",@"selected",@"deselcted",@"deselcted",@"deselcted",@"deselcted",@"deselcted",@"deselcted",@"deselcted",@"deselcted",@"deselcted",nil]; 

for(int i = 0; i< arrProductSelectionB.count-1;i ++) { 
    if ([arrProductSelectionB[i] isEqualToString:@"selected"]) { 
     [arrSelected addObject:arrProductSelection[i]]; 
    } 
} 

NSString *strSelected = [arrSelected componentsJoinedByString:@","]; 

NSLog(@"%@", strSelected);//output: German,Russian,Chinese 
+0

곧 당신을위한 녹색 진드기 –

0

먼저 내가

if (arrProductSelection.count == arrProductSelectionB) { 

//than all you need is one for cycle something like : 

    for (int i = 0; i < arrProductionSelectionB.count; i++) { 
     if ([arrProductionSelectionB[i] isEqualToString: @"selected"]) { 
     do something magically with arrProductSelection[i]; 
    } 
    } 
} 
0

같은 두 배열은 안전 뭔가 같은 카운터를 가지고 있는지 확인합니다.

NSMutableArray*resultArray=[[NSMutableArray alloc]init]; 

    for(int i=0; i<arrProductSelectionB.count;i++){ 
     if ([[arrProductSelectionB objectAtIndex:i]isEqualToString:@"selected"]) { 
      [resultArray addObject:[arrProductSelection objectAtIndex:i]]; 
     } 
    } 

:

(
     { 
     flag = deselcted; 
     name = English; 
    }, 
     { 
     flag = selected; 
     name = German; 
    }, 
     { 
     flag = deselcted; 
     name = Russian; 
    }, 
     { 
     flag = deselcted; 
     name = Chinese; 
    } 
) 

==> 배열 [(사전) (사전), .....]

0

가 배열 될 필요하면, 여기에 함수 resultArray에는 필요한 값이 있습니다.

관련 문제