我有一个对象,如下所示
#import <Foundation/Foundation.h>
@interface CountriesDAO : NSObject
@property (nonatomic, retain) NSString * countryname;
@end
#import "CountriesDAO.h"
@implementation CountriesDAO
@synthesize countryname;
@end
我已将此保存在appDelegate中的Array中。
@property (nonatomic, retain) NSArray *countriesArray;
@synthesize countriesArray;
在另一个控制器中,我像
一样获取它 NSArray *countriesArray = appDelegate.countriesArray; It works fine and I get array.
我试图通过这种方式对其进行排序。
NSArray *countriesArray1 = appDelegate.countriesArray;
NSSortDescriptor *valueDescriptor = [[NSSortDescriptor alloc] initWithKey:@"countryname" ascending:YES];
NSArray * descriptors = [NSArray arrayWithObject:valueDescriptor];
countriesArray = [countriesArray1 sortedArrayUsingDescriptors:descriptors];
我收到错误
[__NSCFType count]: unrecognized selector sent to instance 0xa83b4d0
2013-11-01 13:21:08.882 ECP[13597:c07] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFType count]: unrecognized selector sent to instance 0xa83b4d0'
此处countriesArray定义为
@property (nonatomic, retain) NSArray *countriesArray;
答案 0 :(得分:1)
你也可以试试......
[countriesArray1 sortUsingSelector:@selector(compare:)];
答案 1 :(得分:1)
我是这样做的,它对我有用。
countriesArray = [[NSMutableArray alloc] init];
for (CountriesDAO *info in appDelegate.countriesArray) {
[countriesArray addObject:info.countryname];
}
[countriesArray sortUsingSelector:@selector(localizedCaseInsensitiveCompare:)];
答案 2 :(得分:0)
应该有效
NSMutableArray *sortedArray = [appDelegate.countriesArray mutableCopy];
[sortedArray sortUsingComparator:^NSComparisonResult(CountriesDAO *object1,
CountriesDAO *object2) {
return [object1.countryname caseInsensitiveCompare:object2.countryname];
}];
NSArray *sortedCountriesArray = [sortedArray copy];
如果出现问题,请检查object1
和object2
答案 3 :(得分:0)
它很简单在iOS中对数组进行排序,这对你有帮助
[countriesArray sortUsingComparator:^NSComparisonResult(id obj1, id obj2) {
return [ String_1 compare: String_2];
}];
// String_1, String_2 are NSString,it's contains in countriesArray
// use NSMutableArray to rearrange and change values in countriesArray
答案 4 :(得分:-1)
如果要排序的数组包含NSString(这是我从您的描述中获得的),您可以用以下代码替换您的排序代码:
sortedArray = [countriesArray1 sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)];