2012-09-13 2 views
0

내가 필요한 키워드가 있으면 실제로 내용을 확인하고 표시하고 싶습니다. 찾고있는 키워드가 없다면 표시되지 않습니다.Objective-C에서 특정 키워드를 DIsplay하기 위해 NSDictionary를 필터링하는 방법

예를 들어 도시 사전과 각 도시에는 해당 도시에 대한 설명이 있습니다. 그 설명에 키워드 "쇼핑몰"을 찾고 그 도시에 해당 키워드가 있으면 도시와 설명을 표시해야합니다.

어떻게하면됩니까? 미리 감사드립니다! :)

답변

1

나는 사전을 반복하고 단어 "몰"

NSMutableArray *array = [NSMutableArray array]; 
    for (NSString *aKey in [dict allKeys]) { 
     NSString *desc = [dict valueForKey:aKey]; 
     if ([desc rangeOfString:@"mall"].length == 4) { 
      [array addObject:[NSDictionary dictionaryWithObject:desc forKey:aKey]]; 
     } 
    } 
    //do what you want with array to display the values 
+0

감사를 사용하는 예입니다. 이것은 유망 해 보입니다. 그것을 시도 할 것이다. :) – jaytrixz

0

valueForKey 방법을 찾기 위해 rangeOfString을 사용합니다.

https://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSDictionary_Class/Reference/Reference.html#//apple_ref/occ/instm/NSDictionary/valueForKey : 상황이 경우

당신은 그냥 만들 수 있습니다, 그것은 nil, 디스플레이 아무 것도 반환하지 않는 경우. 그렇지 않으면 리턴 값을 표시하십시오.

다음은 이에 대한

NSString *string = [NSString stringWithString:[dictionary valueForKey:@"mall"]]; if (string) //Send that string to somewhere where it would be visible ;

+0

고마워요. 그것을 시도 할 것이다. :) – jaytrixz

관련 문제