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

标签 iphone objective-c ios xcode nsarray

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

相关文章:

ios - 如何在 iOS 中使用自定义 url 方案传递信息(通过注释)?

iphone - Objective-C中的JSON解析

ios - 点击时清除横幅通知

ios - 设置UITabBar的选中项是否调用tabBar :didSelectItem method?

iphone - 将图像添加到 UiTextView 并发布

objective-c - objective-c - 委托(delegate)和事件

iphone - 核心数据 : The fetched object at index x has an out of order section name 'xxxxxx. 对象必须按节名排序

iphone - .h 文件中声明方法与指定 @interface 和 @end 的 .m 文件中声明方法之间的区别

iphone - 检测添加为 UIScrollView subview 的 UIImage 触摸

ios - 图像的裁剪选项