如何将NSMutableDictionary分成两个NSMutableArrays?

时间:2012-02-14 15:30:15

标签: iphone objective-c nsmutablearray nsmutabledictionary

我试图在比较NSMutableDictionary中的一个keyValue时将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其他明智的添加到mutable array2

3 个答案:

答案 0 :(得分: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;

答案 1 :(得分: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);

当然,如果distance等于500,结果将不会出现在任何一个数组中。

答案 2 :(得分:0)

看起来你正在解析JSON。首先,告诉谁生成了JSON支持数字的数据。 Lat,long和distance应该是数字,而不是字符串。谁使用一个带有大写字母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];