在iphone中实现NSSortDescriptor



我有一个命名为nsmarrMedicineInfo的不可篡改数组。我想用NSSortDescriptor对元素排序。当我执行以下代码时,它显示了NSUnknownKeyException。我找不到数组的键值

NSSortDescriptor *sortDescriptor = [[[NSSortDescriptor alloc] initWithKey:@"sort" ascending:YES selector:@selector(localizedCaseInsensitiveCompare:)] autorelease];
NSArray *sort = [NSArray arrayWithObject:sortDescriptor];
NSArray *sortedArray = [nsmarrMedicineInfo sortedArrayUsingDescriptors:sort];

这里的键将是您的自定义对象数组nsmarrMedicineInfo属性(键值对)之一,您希望对您的数组进行排序。如苹果的文档:

NSSortDescriptor的实例描述了对象排序的基础通过指定用于比较对象的属性,方法使用to 比较属性,以及是否应该进行比较升序或降序

但是如果nsmarrMedicinInfo对象仅由字符串组成,那么您可以使用sortedArrayUsingSelector方法而不是sortDescriptor

谢谢,

相关内容

  • 没有找到相关文章

最新更新