gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 17:32:02 25 4
gpt4 key购买 nike

根据我的要求,我需要对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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com