我这里有两个数组来自另一个类,一个是我在函数中创建的,我的最终目标是让 teamRoster 按字母顺序排序,这似乎可行,但有人可以告诉我是否有更好的方法吗?谢谢。
-(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];
}
最佳答案
有没有更好的方法?取决于您所说的“更好”是什么意思。
您的代码正确且易于理解。
假设 teamRoster
是 NSMutableArray
类型的属性,您可以简单地对其进行排序:
[[appDelegate teamRoster] sortUsingSelector:@selector(caseInsensitiveCompare:)];
但可能出于性能或并发性的原因,不这样做。
另一方面,您可能希望保护 teamRoster
数组不被应用程序委托(delegate)之外的代码修改,在这种情况下,您应该只将其作为不可变的 NSArray 提供,并且您将无法使用上面的方法对其进行排序。
关于iphone - 这是将 objective-c 数组设置为彼此相等的最佳方法吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6415500/