2012-02-14 56 views
-1

我想要一个的NSMutableDictionary上的NSMutableDictionary比较一个的keyValue分为两个NSMutableArrays,如何将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添加到可变数组1其他聪明添加到可变数组2

+0

这看起来不像Dictonary。这看起来像一个字典阵列。 – 2012-02-14 15:42:32

+0

有没有什么办法根据我的查询排序这个? – iPhoneDev 2012-02-14 15:45:31

回答

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 2012-02-14 15:47:10

+0

@iphoneDev你真的不应该使用这个,NSPredicate要快得多。 – 2012-02-14 15:50:00

+0

你可以使用'NSPredicate'方法来限制搜索。但是我认为作者在上面这样的东西上会有足够的麻烦。 – basvk 2012-02-14 15:53:34

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

位置u硬编码是否需要这样做?因为我已经有一个可变数组与关键“距离” – iPhoneDev 2012-02-14 16:05:33

+0

我得到这个错误使用此 - [__ NSCFNumber长度]:无法识别的选择发送到实例0x11cda0 – iPhoneDev 2012-02-14 16:08:24

+0

@iPhoneDev不,你不需要硬编码的长度。如果你已经有一个数组,那么你可以简单地使用'-filteredArrayUsingPredicate:'调用。 – 2012-02-14 17:22:05

0

看起来像你解析JSON。首先,告诉谁生成了JSON支持数字的数据。纬度,长度和距离应该是数字,而不是字符串。谁使用一个大写字母L和一个小写字母l的密钥?这些人不能使用他们的键盘吗?那就是所以要求麻烦,有人使用错误的拼写和搜索小时的错误。

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,除非你有一个很好的理由,你可以解释和捍卫为什么要使用的floatValue不中的doubleValue。

如果您的数据有任何可能性,您的数据的距离为空值,您需要检查,否则您的程序将崩溃。

点不够加评论:

这DOUBLE_OBJ宏是太恶心了。为了得到一个具有数值的NSNumber,请写下例如:

@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];