iphone - NSSortDestiptor 按 plist 编号排序时出错

标签 iphone objective-c ios

我正在使用 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 的其他类方法包括 numberWithLongnumberWithDoubleNumberWithFloat

关于iphone - NSSortDestiptor 按 plist 编号排序时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11940771/

相关文章:

iphone - 当应用 CAAnimation 时,AQGridView 单元格会被交换

iphone - 隐藏后navigationController工具栏不会重新出现

ios - 使用 For 循环在 UISlider 上绘制线条

ios - 如何从 didReceiveRemoteNotification 返回数据中获取数据?

ios - 如何使用 UIPickerview 在两个 View Controller 之间导航

iPhone - 最终确定 Apple 的模糊 "VerificationController.m"

ios - admob ios7 错误音频框架

iphone - "Object 0x84be00 of class NSCFString autoreleased with no pool in place - just leaking"- 但在应用程序的第一行!

ios - 如何检查导航堆栈上的当前 UIViewController

ios - Action script 3.0 问题,我的 swf 无法卸载