gpt4 book ai didi

iphone - 排序 NSArray 并返回 NSArray?

转载 作者:搜寻专家 更新时间:2023-10-30 19:51:08 28 4
gpt4 key购买 nike

我只是想将 NSNumbersNSArray 排序为数字顺序,但我不确定最好的方法。以我的思维方式,001 和 002 具有相当的可比性,所以我怀疑两者都可以。对于 003,我不确定当方法需要 NSArray 时返回 NSMutableArray 是否是一个好习惯,它有效,但感觉很尴尬。

-(NSArray *)testMethod:(NSArray *)arrayNumbers {    
// 001
NSMutableArray *sortedArray = [NSMutableArray arrayWithArray:arrayNumbers];
[sortedArray sortUsingSelector:@selector(compare:)];
arrayNumbers = [NSArray arrayWithArray:sortedArray];
return(arrayNumbers);
}

.

-(NSArray *)testMethod:(NSArray *)arrayNumbers {    
// 002
NSMutableArray *sortedArray = [NSMutableArray arrayWithArray:arrayNumbers];
[sortedArray sortUsingSelector:@selector(compare:)];
arrayNumbers = [[sortedArray copy] autorelease];
return(arrayNumbers);
}

.

-(NSArray *)testMethod:(NSArray *)arrayNumbers {    
// 003
NSMutableArray *sortedArray = [NSMutableArray arrayWithArray:arrayNumbers];
[sortedArray sortUsingSelector:@selector(compare:)];
return(sortedArray);
}

最佳答案

您根本不需要可变数组。你可以这样做:

NSArray* sortedArray = [arrayNumbers sortedArrayUsingSelector:@selector(compare:)];

关于iphone - 排序 NSArray 并返回 NSArray?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2553894/

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