开发者

Is this the best way to set objective c arrays equal to oneanother?

I have two arrays here on is from another class and one i have created in the function, my end goal is to have teamRoster sorted alphabetically, this seems to work but could someone please tell me if there is a better way? Thanks.

-(IBAction)submitAndSendBack:(id)开发者_开发百科sender{
        CoCoachAppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];

        NSString *fullName = [ NSString stringWithFormat:@"%@ %@",firstName.text, lastName.text];   
        [[appDelegate teamRoster] addObject:fullName];
        NSArray *temp = [[appDelegate teamRoster] sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)];

        [[appDelegate teamRoster] removeAllObjects];
        [[appDelegate teamRoster] addObjectsFromArray:temp];


        NSLog(@"%@", [appDelegate teamRoster]);

        [ap

pDelegate.navigationController popViewControllerAnimated:YES];
}


Is there a better way? Depends on what you mean by "better".

Your code is correct and easy enough to understand.

Assuming teamRoster is a property of type NSMutableArray, you could simply sort it in place:

[[appDelegate teamRoster] sortUsingSelector:@selector(caseInsensitiveCompare:)];

But there might be reasons NOT to do it that way, say for performance or concurrency.

On the other hand, you might want to protect the teamRoster array from being modified by code outside of the app delegate, in which case you should only make it available as an immutable NSArray, and you wouldn't be able to use the above to sort it.


How about this?

NSArray *temp = [[appDelegate teamRoster] sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)];
[appDelegate setTeamRoster:temp];

If your app delegate has a teamRoster set up as a property, the setter will let you replace the old array without having to empty it and refill it.


Try this:

appDelegate.teamRoster = [[temp mutableCopy] autorelease];

That is assuming you have a property setup that allows you to change teamRoster.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜