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