gpt4 book ai didi

ios - 对 NSMutableArray 中的 NSString 对象进行排序

转载 作者:行者123 更新时间:2023-11-29 03:00:47 24 4
gpt4 key购买 nike

我在 sqlite 数据库的 AppDelegate.m 中初始化了 NSMutableArray 并填充了 NSString 对象。现在我已经在我的 View Controller 中设置了 AppDelegate:

appDelegate = (IBUAppDelegate *)[[UIApplication sharedApplication] delegate];

所以我可以像这样访问我的 NSMutableArray 对象:

[appDelegate.myNonSortedArray objectAtIndex:row];

现在我已经在我的 View Controller 的@interface 部分创建了一个 NSMutable 数组,这样我就可以使用来自 AppDelegate.m 的 NSMutableArray 中排序的 NSString 对象来填充它。

@interface IBULanguageViewController ()
{
IBUAppDelegate *appDelegate;
NSMutableArray *myArraySorted;
}
@end

然后我尝试在我的 View Controller 中的 - (void)viewDidLoad 方法中使用 AppDelegate.m 中来 self 的 NSMutableArray 的已排序 NSString 来填充 myArraySorted,这样我就可以在我的 TableView Controller 中创建单元格期间访问已排序的数组。

- (void)viewDidLoad
{
[super viewDidLoad];
appDelegate = (IBUAppDelegate *)[[UIApplication sharedApplication] delegate];
myArraySorted = [[NSMutableArray alloc] init];

[myArraySorted addObjectsFromArray:[appDelegate.myNonSortedArray sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)]];
}

我得到 [NSNull localizedCaseInsensitiveCompare:]: unrecognized selector sent to instance 0x1a51068

最佳答案

问题是您的未排序数组中有 NSNull。您需要在调用 sortedArrayUsingSelector: 之前删除它们,实际上数组中的所有内容都必须实现选择器 localizedCaseInsensitiveCompare:(几乎必须是字符串)。您可以通过首先遍历数组并调用 [obj repsondsToSelector:@selector (localizedCaseInsensitiveCompare:] 任何不响应的内容来检查选择器是否响应选择器将破坏排序方法。

关于ios - 对 NSMutableArray 中的 NSString 对象进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23323033/

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