简体   繁体   English

NSMutableArray到NSArray排序

[英]NSMutableArray to NSArray sort

i'm trying to sort my core data object by date. 我正在尝试按日期对我的核心数据对象进行排序。 but i keep getting this error: 但我不断收到此错误:

incompatible pointer types assigning to 'nsmutablearray *' from 'nsarray *'    

This is the code that i've made. 这是我编写的代码。 All my other code is based on a nsmutablearray, so its important the output is a nsmutablearray. 我所有其他代码均基于nsmutablearray,因此其重要的输出是nsmutablearray。 How can i do this without getting the error above? 我如何做到这一点而没有得到上面的错误?

devices = [devices2 sortedArrayUsingDescriptors:[NSArray arrayWithObject:[NSSortDescriptor sortDescriptorWithKey:@"created" ascending:YES]]];

you can use mutableCopy function. 您可以使用mutableCopy函数。 So like

 devices = [[devices2 sortedArrayUsingDescriptors:[NSArray arrayWithObject:[NSSortDescriptor sortDescriptorWithKey:@"created" ascending:YES]]] mutableCopy];

NSMutableArray has an alternative to sortedArrayUsingDescriptors , which sorts the array in place, instead of returning a new immutable array. NSMutableArray可以替代sortedArrayUsingDescriptors ,它可以对数组进行适当排序,而不是返回新的不可sortedArrayUsingDescriptors组。

You can use: 您可以使用:

[devices2 sortUsingDescriptors:@[ [NSSortDescriptor sortDescriptorWithKey:@"created" ascending:YES] ]];

Otherwise, as @Ethan stated above, you can create a mutableCopy of the resulting array. 否则,如上面的@Ethan所述,您可以创建结果数组的mutableCopy

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM