iphone - 我们可以一次使用排序描述符和@distinctUnionOfObjects吗?

标签 iphone ios nsmutablearray nssortdescriptor

根据我的要求,我需要对NSMutableArray进行排序,并需要在该数组中放置唯一值。

这是我的代码,但是我遇到一个问题,我得到两个数组:一个数组具有不同的值,另一个数组没有不同的值但已排序。

/ *Making the years unique so that it will display only once*/
        mSortingArray = [mYearsArray valueForKeyPath:@"@distinctUnionOfObjects.self"] ;
        NSLog(@"mSortingArray%@",mSortingArray);

        /*Sorting the array to get the years in assending format*/
        NSSortDescriptor *sortDescriptor;
        sortDescriptor = [[NSSortDescriptor alloc]initWithKey:@"self"ascending:YES];
        NSArray *sortDescriptors = [NSArray arrayWithObject:sortDescriptor];
        [mYearsArray sortUsingDescriptors:sortDescriptors];
        NSLog(@"%@ :%@",mYearsArray,sortDescriptors);

如何在一个数组中获得两个结果?

最佳答案

您不能在单个操作中完成此操作,但是可以将排序描述符应用于唯一操作的结果:

 NSArray *uniqueArray = [mYearsArray valueForKeyPath:@"@distinctUnionOfObjects.self"];
 NSArray *sortedUniqueArray = [uniqueArray sortedArrayUsingDescriptors:sortDescriptors];
 mYearsArray = [sortedUniqueArray mutableCopy];

关于iphone - 我们可以一次使用排序描述符和@distinctUnionOfObjects吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18056602/

相关文章:

ios - Swift instagram 克隆 : loading followed user post

ios - 从解析中检索到的数据顺序

iphone - EXC_BAD_ACCESS 带有 characterAtIndex,但不是 substringToIndex

ios - Iphone 5 Appium 和 jUnit - 应用程序没有元素

ios - 如何在 iPhone X 的主页指示器下方扩展底部 View 颜色?

ios - UIPickerView - 不适用于 UITextView TouchUp

objective-c - 如何在 NSMutableArray 中定位最大值和位置

iphone - 为什么 CGImageCreateWithMaskingColors() 在这种情况下返回 nil?

javascript - Cordova:无论如何在 Javascript 中检测 iOS 应用程序是作为调试还是发布构建的?

ios - 根据来自后端的数据为 UITableView 中的部分设置数据源