gpt4 book ai didi

ios - 排序键和值

转载 作者:行者123 更新时间:2023-11-28 22:53:35 28 4
gpt4 key购买 nike

我在从 IOS 的属性列表中排序键时遇到困难。

我正在将列表加载到字典中,然后从中提取键到数组,并使用以下方法对它们进行排序:“sortedArrayUsingSelector'selector(compare:)”

当我尝试使用键时,值似乎没有按照键排序?示例:(键值)未排序:阿森斯 0、欧登塞 1、尼堡 1、米德尔法特 0。排序:阿森斯,1,欧登塞 1,尼堡 0,米德尔法特 0。

`

NSBundle *bundle = [NSBundle mainBundle];
NSURL *KSplistURL = [bundle URLForResource:@"KommunerStedtillæg"
withExtension:@"plist"];
NSDictionary *dictionary = [NSDictionary
dictionaryWithContentsOfURL:KSplistURL];
self.kommuner = dictionary;

//NSArray *keys = [[self.kommuner allKeys] sortedArrayUsingSelector:@selector(compare)];

NSArray *components = [self.kommuner allKeys];
NSArray *keys = [components sortedArrayUsingSelector:@selector(compare:)];


NSMutableDictionary *newDict = [NSMutableDictionary dictionary];
for (id key in keys)[newDict setObject:[dictionary objectForKey:key]forKey:key];




self.kommunerKeys = [newDict allKeys];
self.kommunerValues = [newDict allValues];

`

最佳答案

为什么你认为字典会知道你正在对它的键数组的一个无关副本进行排序?你必须像这样创建另一个字典:

NSArray *keys = [[originalDict allKeys] sortedArrayUsingSelector:@selector(compare:)];
NSMutableDictionary *newDict = [NSMutableDictionary dictionary];
for (id key in keys)
[newDict setObject:[originalDict objectForKey:key] forKey:key];

现在 newDict 包含排序的键 值。

关于ios - 排序键和值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11288755/

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