Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/cocoa/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/amazon-s3/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c 根据NSMutable数组中的内容对NSMutableDictionary排序_Objective C_Cocoa - Fatal编程技术网

Objective c 根据NSMutable数组中的内容对NSMutableDictionary排序

Objective c 根据NSMutable数组中的内容对NSMutableDictionary排序,objective-c,cocoa,Objective C,Cocoa,我有一个数组NSMutableArray,其值为 10,2,13,4 还有NSMutableDictionary,其中的值是 (10,a)、(20,b)、(13,c)、(2,d)、(33,e) 我想对NSMutableDictionary中的值进行排序,使dict的结果应该是(10,a)、(2,d)、(13,c)我为您编写的函数。希望它能帮助您: - (void)removeUnnedful { NSMutableDictionary *dict = [[NSMutableDiction

我有一个数组
NSMutableArray
,其值为 10,2,13,4

还有
NSMutableDictionary
,其中的值是 (10,a)、(20,b)、(13,c)、(2,d)、(33,e)


我想对
NSMutableDictionary
中的值进行排序,使dict的结果应该是(10,a)、(2,d)、(13,c)

我为您编写的函数。希望它能帮助您:

- (void)removeUnnedful
{
    NSMutableDictionary *dict = [[NSMutableDictionary alloc] initWithObjectsAndKeys:
                          @"a", [NSNumber numberWithInt:10],  
                          @"b", [NSNumber numberWithInt:20], 
                          @"c", [NSNumber numberWithInt:13], 
                          @"d", [NSNumber numberWithInt:2 ],
                          @"e", [NSNumber numberWithInt:33],  
                          nil];
    NSMutableArray *array = [[NSMutableArray alloc] initWithObjects:
                             [NSNumber numberWithInt:10],
                             [NSNumber numberWithInt:2 ],
                             [NSNumber numberWithInt:13],
                             [NSNumber numberWithInt:14], nil];

    NSMutableDictionary *newDict = [[NSMutableDictionary alloc] init];
    for (NSNumber *key in [dict allKeys])
    {
        NSLog(@"%@", key);
        if ([array containsObject:key])
        {
            [newDict setObject:[dict objectForKey:key] forKey:key];
        }
    }

    for (NSNumber *key in [newDict allKeys])
        NSLog(@"key: %@, value: %@", key, [newDict objectForKey:key]);

    [dict release];
    [array release];
}

未定义
NSDictionary
实例中键和值的排序顺序。(见)
由于您已经拥有一个有序键数组,您可以简单地迭代该数组并访问该键的字典值:

NSMutableArray* sortedArray = [NSMutableArray arrayWithObjects:@"10", @"2", @"13", @"4", nil];
NSDictionary* dictionary = [NSDictionary dictionaryWithObjectsAndKeys:@"a", @"10", @"b", @"20", @"c", @"13", @"d", @"2", @"e", @"33" , nil];
NSMutableDictionary* filteredDictionary = [NSMutableDictionary dictionary];
for(id key in sortedArray)
{
    id value = [dictionary objectForKey:key];
    if(value != nil)
    {
        [filteredDictionary setObject:[dictionary objectForKey:key] forKey:key];
    }
}
NSLog(@"%@", filteredDictionary);

请注意,
[NSDictionary description]
的默认实现按键对输出进行升序排序(对于类型为
NSString
的键),但这只是一个表示-
nsdictionary
没有定义的排序顺序,因此您不应该依赖于
allkey
allValues

的排序。是否要从dictionary中删除不在数组中的值?查看我的答案,希望对您有所帮助。