本文介绍了排序NSArray和返回NSArray?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是想把 NSArray NSNumbers 排序为数字顺序,但有点不确定最好的方式去。通过我的思考方式,001和002是相当可比的,所以我怀疑要么会做。对于003我不知道如果返回 NSMutableArray 当方法期望 NSArray 是好的做法,它的工作,但感觉尴尬。

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:)];

这篇关于排序NSArray和返回NSArray?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-22 16:56