2

I have an object, as below

#import <Foundation/Foundation.h>

@interface CountriesDAO : NSObject

@property (nonatomic, retain) NSString * countryname;

@end



#import "CountriesDAO.h"

@implementation CountriesDAO
@synthesize countryname;

@end

I have saved this in Array within appDelegate.

@property (nonatomic, retain) NSArray *countriesArray;

@synthesize countriesArray;

In another controller, I fetch it like

  NSArray *countriesArray = appDelegate.countriesArray; It works fine and I get array.

As I try to sort it by this way.

 NSArray *countriesArray1 = appDelegate.countriesArray;
    NSSortDescriptor *valueDescriptor = [[NSSortDescriptor alloc] initWithKey:@"countryname" ascending:YES];
    NSArray * descriptors = [NSArray arrayWithObject:valueDescriptor];

    countriesArray = [countriesArray1 sortedArrayUsingDescriptors:descriptors];

I get error

[__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'

Here countriesArray is defined as

 @property (nonatomic, retain) NSArray *countriesArray;
5
  • Seems like countriesArray is not an array. Commented Nov 1, 2013 at 8:30
  • How its not an Array, @property (nonatomic, retain) NSArray *countriesArray; Commented Nov 1, 2013 at 8:32
  • Can you show the code where you initialize the CountriesDAO-objects and put them in the array? This looks correct to me. Commented Nov 1, 2013 at 8:40
  • What you are storing into the array countryname or object of class CountriesDAO? Commented Nov 1, 2013 at 8:52
  • What makes you think that declaring countriesArray as an NSArray* makes it an NSArray? An object is whatever it is. Just because you assign it to an NSArray* doesn't make it one. And quite obviously, it isn't an NSArray. That's why you get an exception. Look at the code in the delegate. Commented Apr 10, 2014 at 11:48

5 Answers 5

1

I did it like this and it worked for me.

countriesArray = [[NSMutableArray alloc] init];


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

[countriesArray sortUsingSelector:@selector(localizedCaseInsensitiveCompare:)];
Sign up to request clarification or add additional context in comments.

Comments

1

You can also try...

[countriesArray1 sortUsingSelector:@selector(compare:)];

3 Comments

[Countries compare:]: unrecognized selector sent to instance 0x8c63cd0'
That won't work. His code is correct, it's probably something that went wrong when he puts the objects in the original array
NSArray *countriesArray = [[NSArray alloc] init]; NSArray *countriesArray1 = [[NSArray alloc] initWithObjects:@"3",@"2",@"4",@"5",@"1", nil]; countriesArray = [countriesArray1 sortedArrayUsingSelector:@selector(compare:)]; NSLog(@"%@",countriesArray);
0

Should work

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

If something went wrong, check the type of object1 and object2

Comments

0

Its is simple Way to sort array in iOS,it's helps to you

[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

Comments

-1

If the array you want to sort contains NSString (which is what I get from your description) you can replace your sorting code with this:

sortedArray = [countriesArray1 sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)];

5 Comments

Countries localizedCaseInsensitiveCompare:]: unrecognized selector sent to instance CRASH ........
Change NSArray *countriesArray1 = appDelegate.countriesArray; with NSMutableArray *countriesArray1 = [countriesArray1 addObjectsFromArray:appDelegate.countriesArray;
The array contains CountriesDAO objects which have a countryname property
This can't be a way to init NSMutableArray, two time countriesArray1 and what do you want to do by it?
@Accatyyc, Yes, Array contains object, one more thing also comes in between, thats database,

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.