2

我有一个对象,如下

#import <Foundation/Foundation.h>

@interface CountriesDAO : NSObject

@property (nonatomic, retain) NSString * countryname;

@end



#import "CountriesDAO.h"

@implementation CountriesDAO
@synthesize countryname;

@end

我已将其保存在 appDelegate 中的数组中。

@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'

这里国家数组定义为

 @property (nonatomic, retain) NSArray *countriesArray;
4

5 回答 5

1

你也可以试试...

[countriesArray1 sortUsingSelector:@selector(compare:)];
于 2013-11-01T08:37:46.940 回答
1

我这样做了,它对我有用。

countriesArray = [[NSMutableArray alloc] init];


for (CountriesDAO *info in appDelegate.countriesArray) {
    [countriesArray addObject:info.countryname];
}    

[countriesArray sortUsingSelector:@selector(localizedCaseInsensitiveCompare:)];
于 2013-11-01T09:00:26.010 回答
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
于 2013-11-01T08:55:33.353 回答
0

应该管用

NSMutableArray *sortedArray = [appDelegate.countriesArray mutableCopy];
[sortedArray sortUsingComparator:^NSComparisonResult(CountriesDAO *object1, 
                                                     CountriesDAO *object2) {                
    return [object1.countryname caseInsensitiveCompare:object2.countryname];
}];
NSArray *sortedCountriesArray = [sortedArray copy];

如果出现问题,请检查类型object1object2

于 2013-11-01T08:47:51.957 回答
-1

如果要排序的数组包含 NSString (这是我从你的描述中得到的),你可以用这个替换你的排序代码:

sortedArray = [countriesArray1 sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)];
于 2013-11-01T08:33:41.267 回答