gpt4 book ai didi

iphone - 在 iPhone 中实现 NSSortDescriptor?

转载 作者:行者123 更新时间:2023-12-03 21:18:13 24 4
gpt4 key购买 nike

我有一个名为 nsmarrMedicineInfo 的 nsmutablearray。我想使用 NSSortDescriptor 对元素进行排序。当我执行以下代码时,它显示 NSUnknownKeyException。我找不到数组的键是什么。

NSSortDescriptor *sortDescriptor = [[[NSSortDescriptor alloc] initWithKey:@"sort" ascending:YES selector:@selector(localizedCaseInsensitiveCompare:)] autorelease];
NSArray *sort = [NSArray arrayWithObject:sortDescriptor];
NSArray *sortedArray = [nsmarrMedicineInfo sortedArrayUsingDescriptors:sort];

最佳答案

此处的键将是您希望的数组 nsmarrMedicineInfo自定义对象的属性(键值对)之一对数组进行排序。正如苹果的文档中所示:

An instance of NSSortDescriptor describes a basis for ordering objects by specifying the property to use to compare the objects, the method to use to compare the properties, and whether the comparison should be ascending or descending.

但是,如果 nsmarrMedicinInfo 对象仅由字符串组成,那么您可以使用 sortedArrayUsingSelector 方法而不是 sortDescriptor

谢谢

关于iphone - 在 iPhone 中实现 NSSortDescriptor?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8305848/

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