gpt4 book ai didi

objective-c - 根据 NSMutable 数组中的内容对 NSMutableDictionary 进行排序

转载 作者:行者123 更新时间:2023-12-03 17:06:16 26 4
gpt4 key购买 nike

我有一个数组 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];
}

关于objective-c - 根据 NSMutable 数组中的内容对 NSMutableDictionary 进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7346160/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com