2013-06-21 3 views
-3

내가 확인내가 확인할 수있는 방법을 개체가 이미

[mutableArry addObject:[NSNumber numberWithInt:questionNumber]]; 
    NSLog(@"mutableArry=%@",mutableArry); 
     NSLog(@" question not complete count=%d",mutableArry.count); 

    if (mutableArry.count==25) { 
     NSLog(@"25 question complete%d",mutableArry.count); 
    } 

    NSString *str= [NSString stringWithFormat:@"%d",questionNumber]; 
    NSLog(@"str=%@",str); 

    // array = [NSArray arrayWithObjects: @"Nicola", @"Margherita", @"Luciano", @"Silvia", nil]; 
    if ([mutableArry containsObject:str]) { //YES 
    NSLog(@"Results"); 
    } 
+0

'NSString' str과 NSMutableArray' mutableArry를 호출하는 것이 코드에서 잘 보이지 않습니다. – Kuba

답변

3

잘못하여 두 줄을 무엇을하고 있는가 배열에 존재 questionNumber를 NS로 추가하십시오. 문자열 또는 숫자를 조회 : 더 성공을 가져다해야한다)

NSMutableArray *ary = [NSMutableArray arrayWithObject:[NSNumber numberWithInt:25]]; 
NSString *str= [NSString stringWithFormat:@"%d",25]; 

if([ary containsObject:[NSNumber numberWithInt:25]]) 
    NSLog(@"ary contains object - number"); 

if([ary containsObject:str]) 
    NSLog(@"ary contains object - string"); 
else 
    NSLog(@"ary doesn't contain object - string"); 

로그 :
워 개체 포함 - 수
워 객체가 포함되어 있지 않습니다 - 문자열 당신은 시도

+0

덕분에이 문제가 해결되었습니다 –

3

을 두 개의 다른 대상을 비교하십시오. str 개체는 배열에 넣은 개체가 아니므로 비교할 수 있습니다.

그래서 당신은이 작업을 수행 할 수 있습니다

for(NSString *stringFromArr in mutableArry) { 
    if([stringFromArr isEqualToString:str]) 
    NSLog(@"Results"); 
} 

을 참고 : 귀하의 이름 지정 규칙이 좋지 않다 ...

+2

당신은 동일한 대답으로 나보다 28 초 빠릅니다. +1 –

0

을 당신이해야하는 대신 if 문 마지막의 다음.

for (NSString *object in mutableArry) { 
    if (object isEqualToString:str) { 
     NSLog(@"Match"); 
    } 
} 
1
if ([mutableArry containsObject:[NSNumber numberWithInt:questionNumber]]) 
{ 
    NSLog(@"Results"); 
} 
0

다음으로 코드를 교체합니다. NSNumber 인스턴스를 추가하고 NSString을 (를) 비교 중입니다.

[mutableArry addObject:[[NSNumber numberWithInt:questionNumber]stringValue]]; 
NSLog(@"mutableArry=%@",mutableArry); 
NSLog(@" question not complete count=%d",mutableArry.count); 

if (mutableArry.count==25) { 
    NSLog(@"25 question complete%d",mutableArry.count); 
} 

NSString *str= [NSString stringWithFormat:@"%d",questionNumber]; 
NSLog(@"str=%@",str); 

// array = [NSArray arrayWithObjects: @"Nicola", @"Margherita",          @"Luciano", @"Silvia", nil]; 
if ([mutableArry containsObject:str]) // YES 
{ 
    NSLog(@"Results"); 
} 
관련 문제