2012-02-14 4 views
-1

내가 NSMutableDictionary 한 키 값을 비교하는 두 NSMutableArrays에 NSMutableDictionary를 분할하는 것을 시도하고,NSMutableDictionary를 두 개의 NSMutableArrays로 나누는 방법은 무엇입니까?

예 : 내 NSMutableDictionary

[ 
     { 
      "0": "87", 
      "1": "13270690451", 
      "2": "Delhi night's", 
      "3": "2106", 
      "4": ":)", 
      "5": "Kunwar", 
      "6": "28.601736", 
      "7": "77.159178", 
      "8": "16.107459108715", 
      "timeleft": "87", 
      "imageurl": "13270690451", 
      "beep": "Delhi night's", 
      "beepid": "2106", 
      "beepdescription": ":)", 
      "username": "Kunwar", 
      "Lat": "28.601736", 
      "long": "77.159178", 
      "distance": "16.107459108715" 
     }, 
     { 
      "0": "87", 
      "1": "13278710651", 
      "2": "Delhi IT hub", 
      "3": "2145", 
      "4": "LPT certification centre", 
      "5": "Kunwar", 
      "6": "28.491764", 
      "7": "77.082712", 
      "8": "2005.6281723630008", 
      "timeleft": "87", 
      "imageurl": "13278710651", 
      "beep": "Delhi IT hub", 
      "beepid": "2145", 
      "beepdescription": "LPT certification centre", 
      "username": "Kunwar", 
      "Lat": "28.491764", 
      "long": "77.082712", 
      "distance": "2005.6281723630008" 
     } 
] 

내가 두 개의 별도 mutablearray을 원하는 경우 "거리": "2005.6281723630008" 사전에 500 미만 mutable array1에 추가 현명한 array2에 추가하십시오

+0

그건 Dictonary처럼 보이지 않습니다. 마치 사전 배열과 같습니다. –

+0

내 쿼리에 따라 이것을 정렬 할 수있는 방법이 있습니까 ?? – iPhoneDev

답변

1

이런 식으로 뭔가?

NSArray *sortedArray = [originalArray sortedArrayUsingComparator:^(id a, id b) { 
    float fa = [[(NSDictionary*)a objectForKey:@"distance"] floatValue]; 
    float fb = [[(NSDictionary*)b objectForKey:@"distance"] floatValue]; 
    NSNumber *first = [NSNumber numberWithFloat:fa]; 
    NSNumber *second = [NSNumber numberWithFloat:fb]; 

    return [first compare:second]; 
}]; 

NSMutableArray *arr1 = [[NSMutableArray alloc] init]; 
NSMutableArray *arr2 = [[NSMutableArray alloc] init]; 

for (NSDictionary *dic in sortedArray) { 
    if ([[dic objectForKey:@"distance"] floatValue] < 500.0f) { 
     [arr1 addObject:dic]; 
    } else { 
     [arr2 addObject:dic]; 
    } 
} 
originalArray = nil; 
+0

originalArray 무엇입니까? – iPhoneDev

+0

@iphoneDev 정말로 이것을 사용하면 안되며, NSPredicate는 훨씬 빠릅니다. –

+0

'NSPredicate'방식으로 검색을 제한 할 수 있습니다. 그러나 저자는 위와 같은 문제에 충분한 문제가 있다고 생각합니다. – basvk

1

NSPredicate이 작업을 위해 만들어진, 당신은 다음과 같이 사용할 수 있습니다 : 물론

#define DOUBLE_OBJ(x) [NSNumber numberWithDouble:x] 
NSDictionary *location1 = [NSDictionary dictionaryWithObject:DOUBLE_OBJ(16.107459108715) forKey:@"distance"]; 
NSDictionary *location2 = [NSDictionary dictionaryWithObject:DOUBLE_OBJ(2005.6281723630008) forKey:@"distance"]; 
NSDictionary *location3 = [NSDictionary dictionaryWithObject:DOUBLE_OBJ(250) forKey:@"distance"]; 
NSDictionary *location4 = [NSDictionary dictionaryWithObject:DOUBLE_OBJ(750) forKey:@"distance"]; 

NSMutableArray *locations = [NSMutableArray arrayWithObjects:location1, location2, location3, location4, nil]; 

NSArray *locationsLessThan500 = [locations filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"distance < 500"]]; 
NSArray *locationsGreaterThan500 = [locations filteredArrayUsingPredicate:[NSPredicate predicateWithFormat:@"distance > 500"]]; 

NSLog(@"locations: %@", locations); 
NSLog(@"locations less than 500: %@", locationsLessThan500); 
NSLog(@"locations greater than 500: %@", locationsGreaterThan500); 

, 거리가 500과 동일한 경우는, 결과는 두 배열에 표시되지 않습니다.

+0

위치는 하드 코드해야합니까? 나는 이미 "distance"라는 키가있는 변경 가능한 배열을 가지고 있기 때문에 – iPhoneDev

+0

- [__ NSCFNumber length] : 인스턴스 0x11cda0로 전송 된 인식 할 수없는 선택자 – iPhoneDev

+0

@iPhoneDev 아니요, 길이를 하드 코딩 할 필요가 없습니다. 이미 배열이 있다면'-filteredArrayUsingPredicate :'호출을 사용하면됩니다. –

0

JSON을 구문 분석하는 것처럼 보입니다. 먼저 JSON에서 숫자를 지원하는 데이터를 만든 사람이 누구인지 말해보십시오. 위도, 경도 및 거리는 문자열이 아니라 숫자 여야합니다. 그리고 누가 대문자 L과 함께 하나의 키를 사용하고 소문자 l과 함께 하나의 키를 사용합니까? 이 사람들은 키보드를 사용할 수 없습니까? 이므로 사람에게 문의하십시오. 은 잘못된 맞춤법을 사용하여 버그 시간을 검색합니다. 당신이 설명하고 당신이하는 FloatValue 아니라있는 doubleValue를 사용하는 이유를 방어 할 수있는 좋은 이유가없는

NSMutableArray* shortDistance = [NSMutableArray array]; 
NSMutableArray* longDistance = [NSMutableArray array]; 

for (NSDictionary* dict in myArray) 
{ 
    if (dict [@"distance"].doubleValue < 500.0) 
     [shortDistance addObject:dict]; 
    else 
     [longDistance addObject:dict]; 
} 

는하는 FloatValue를 사용하지 마십시오.

데이터에 거리 값이 null 일 가능성이있는 경우이를 확인해야합니다. 그렇지 않으면 프로그램이 중단됩니다.

충분하지 않은 점은 아직 설명을 추가합니다 : DOUBLE_OBJ 매크로 그냥 끔찍

있다. 값이있는 NSNumber를 가져 오려면 예를 들어 012W를 작성하십시오.

@100 // Same as [NSNumber numberWithInt:100] 
@12.34 // Same as [NSNumber numberWithDouble:12.34] 
double x = ...; double y = ...; @(x + y) // Same as [NSNumber numberWithDouble:x + y] 

Objective-C 언어의 일부입니다. 그리고 당신은 쓸 수 있습니다

NSDictionary *location1 = { @"distance": @16.107459108715 }; 
NSDictionary *location2 = { @"distance": @2005.6281723630008 }; 
NSDictionary *location3 = { @"distance": @250 }; 
NSDictionary *location4 = { @"distance": @750 }; 

NSArray *locations = @[location1, location2, location3, location4]; 
관련 문제