Question

I have NSMutableArray initialised and populated with NSString objects in AppDelegate.m from sqlite database. Now I've setup that AppDelegate in my View Controller :

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

so I can access my NSMutableArray objects like this:

[appDelegate.myNonSortedArray objectAtIndex:row];

Now I have created one NSMutable array in my @interface part of my View Controller so I can populate it with sorted NSString objects from my NSMutableArray from AppDelegate.m.

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

Then I tried to populate myArraySorted using sorted NSStrings from my NSMutableArray from AppDelegate.m in - (void)viewDidLoad method in my View Controller, so I can access sorted array during creation of cells in my Table View Controller.

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

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

and I get [NSNull localizedCaseInsensitiveCompare:]: unrecognized selector sent to instance 0x1a51068.

Was it helpful?

Solution

The problem is that you are have NSNulls in your unsorted array. you need to remove these before calling sortedArrayUsingSelector: in fact everything in your array must implement the selector localizedCaseInsensitiveCompare: (pretty much must be a string). You can check that everything responds to the selector by going through the array first and calling [obj repsondsToSelector:@selector (localizedCaseInsensitiveCompare:] anything that doesn't respond to this will break the sorting method.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top