我正在使用 NSSortDescriptor
对我的 tableView 进行排序,但这一行造成了麻烦:
if (buttonIndex == 0) {
sortDesc = [[NSSortDescriptor alloc] initWithKey:@"Rate" ascending:NO];
}
“Rate”是 plist(带字典的数组)中的数字字符串。
如果字典未分级,默认值为“0”,如果已分级,则值为“1”、“2”、“3”、“4”、“5”或“6”。为什么我不能按这些值排序,如何排序?
'NSInvalidArgumentException', reason: '-[__NSCFNumber length]: unrecognized selector sent to instance 0x6c382c0'
编辑:
问题是我用字符串而不是数字对字典进行评分:
[mutDict setValue:@"0" forKey:@"Rate"];
如何为 Rate 设置数字而不是字符串?
最佳答案
How to set a number for Rate instead of string?
您可以使用 NSNumber
代替字符串文字:
[mutDict setValue:[NSNumber numberWithInt:0] forKey:@"Rate"];
// The actual rate goes here ----^
NSNumber
的其他类方法包括 numberWithLong
、numberWithDouble
和 NumberWithFloat
。
关于iphone - NSSortDestiptor 按 plist 编号排序时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11940771/