gpt4 book ai didi

iphone - 这是将 objective-c 数组设置为彼此相等的最佳方法吗?

转载 作者:行者123 更新时间:2023-11-29 13:48:07 25 4
gpt4 key购买 nike

我这里有两个数组来自另一个类,一个是我在函数中创建的,我的最终目标是让 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];
}

最佳答案

有没有更好的方法?取决于您所说的“更好”是什么意思。

您的代码正确且易于理解。

假设 teamRosterNSMutableArray 类型的属性,您可以简单地对其进行排序:

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

但可能出于性能或并发性的原因,不这样做。

另一方面,您可能希望保护 teamRoster 数组不被应用程序委托(delegate)之外的代码修改,在这种情况下,您应该只将其作为不可变的 NSArray 提供,并且您将无法使用上面的方法对其进行排序。

关于iphone - 这是将 objective-c 数组设置为彼此相等的最佳方法吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6415500/

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