按日期属性

时间:2017-12-28 08:14:21

标签: ios objective-c nsmutablearray nssortdescriptor

我有一个NSMutableArray,它有一个内部NSMutableArray,我想用内部NSMutableArray中的日期属性对其排序(对外部项进行排序)。

The array looks like this
[
  {
     messages : [
     { 
     date
     },
     ..... etc
     ]
  },
..... etc
]

以下代码是我尝试抛出异常

NSSortDescriptor *sdSortDate = [NSSortDescriptor sortDescriptorWithKey:@"messages.@lastObject.date" ascending:YES];
events = [NSMutableArray arrayWithArray:[events sortedArrayUsingDescriptors:@[sdSortDate]]];

示例

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '[<__NSArrayM 0x604000654670> valueForKeyPath:]: this class does not implement the lastObject operation.'

关于我如何做到这一点的任何想法?

1 个答案:

答案 0 :(得分:1)

您应该使用messages.date.@lastObject代替messages.@lastObject.date。请尝试以下代码

NSSortDescriptor *sdSortDate = [NSSortDescriptor sortDescriptorWithKey:@"messages.date.@lastObject" ascending:YES];
events = [NSMutableArray arrayWithArray:[events sortedArrayUsingDescriptors:@[sdSortDate]]];