我创建了一个包含键 1 到 11 的属性列表,所有键的值为 0。
该列表将用作选择器 View 的数据源。
我有这些属性:
@property (nonatomic, strong) NSDictionary *erfaring;
@property (nonatomic, strong) NSArray *erfaringKeys;
在实现文件中:
NSString *erfaringFile = [[NSBundle mainBundle]
pathForResource:@"Erfaring" ofType:@"plist"];
erfaring = [[NSDictionary alloc] initWithContentsOfFile:erfaringFile];
erfaringKeys = [erfaring allKeys];
问题是,当键被加载到选择器 View 中时,它们的排序是错误的,例如:7、3、8、4、11、9、5、1、6、2、10。
有什么帮助吗?
最佳答案
你需要对字典的键进行排序
NSDictionary *dic = [NSDictionary dictionaryWithObjectsAndKeys:
@"String 1", @"1",
@"String 4", @"4",
@"String 3", @"3",
@"String 2", @"2", nil];
NSArray *sortedKeys = [dic keysSortedByValueUsingSelector:@selector(compare:)];
然后使用排序键进行迭代
for (NSString *sortedKey in sortedKeys) {
NSString *value = [dic objectForKey:sortedKey];
}
关于ios - 在 iOS 的属性列表中排序键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11242449/