【问题标题】:Implementing NSSortDescriptor in iphone?在 iphone 中实现 NSSortDescriptor?
【发布时间】:2012-01-08 12:03:53
【问题描述】:

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

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

【问题讨论】:

    标签: iphone objective-c nsarray nssortdescriptor


    【解决方案1】:

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

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

    但如果nsmarrMedicinInfo 对象只包含字符串,那么您可以使用sortedArrayUsingSelector 方法而不是sortDescriptor

    谢谢,

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-02-25
      • 2010-11-08
      • 2013-03-14
      • 1970-01-01
      • 2011-04-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多