如何将 NSMutableDictionary 分成两个 NSMutableArray?

发布于 2025-01-05 01:41:15 字数 1494 浏览 1 评论 0原文

我试图将 NSMutableDictionary 分成两个 NSMutableArrays 来比较 NSMutableDictionary 中的一个 keyValue,

例如:我的 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"
       }
]

我想制作两个单独的可变数组,如果字典中的“距离”:“2005.6281723630008”小于 500 添加到可变数组 1 否则添加到可变数组 2

I am trying to divide a NSMutableDictionary into two NSMutableArrays on comparing one keyValue in NSMutableDictionary,

example:My 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"
       }
]

i want make two seperate mutablearray if "distance": "2005.6281723630008" in dictionary is less than 500 add to mutable array1 other wise add to mutable array2

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

内心激荡 2025-01-12 01:41:15

像这样的东西吗?

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;

Something like this?

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;
江湖正好 2025-01-12 01:41:15

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,则结果不会出现在任一数组中。

NSPredicate was made for this task, you can use it like this:

#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);

Of course, if distance is equal to 500, the result will not appear in either array.

菩提树下叶撕阳。 2025-01-12 01:41:15

看起来您正在解析 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];

Looks like you're parsing JSON. First, tell whoever produced that data that JSON supports numbers. Lat, long and distance should be numbers, not strings. And who uses one key with an uppercase L and one key with a lowercase l? Can't these people use their keyboards? That is so asking for trouble, where someone will use the wrong spelling and search for hours for the bug.

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

Never use floatValue, unless you have a good reason that you can explain and defend why you would use floatValue and not doubleValue.

If there is any possibility that your data has null values for the distance, you'll need to check for that or your program will crash.

Not enough points to add comments yet:

That DOUBLE_OBJ macro is just awful. To get an NSNumber with a value, write for example

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

It's part of the Objective-C language. And you write

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

NSArray *locations = @[location1, location2, location3, location4];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文