I am just looking at sorting an NSArray
of NSNumbers
into numeric order but am a little unsure of the best way to go. By my way of thinking 001 and 002 are pretty comparable, so I would suspect either will do. For 003 I am not sure if returning NSMutableArray
when the method expects NSArray
is good practice, it works, but it feels awkward.
-(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);
}
You don't need a mutable array at all. You can just do:
NSArray* sortedArray = [arrayNumbers sortedArrayUsingSelector:@selector(compare:)];
我想你可以打电话
return [arrayNumbers sortedArrayUsingSelector:@selector(compare:)];
NSArray * sortedArray = [arrayNumbers sortedArrayUsingSelector:@selector(compare :)];
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.