0
votes

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.

1
One of the 'strings' in your array is in fact an NSNull which doesn't respond to the selector (at least that's what the error message says!) - James Snook
Yes, that's because some records in database have null value. I have got rid of them by using if(![someRecord isEqualToString:@"(null)"]), so that I can add someRecord to my NSMutableArray. As far as I can see there is no NSNull in my array. How can I check that? - user3578142
If someRecord genuinely is an NSNull then then that check won't work because NSNull doesn't respond to [someRecord isEqualToString:@"(null)"] intend to check if the value is an NSNull you'd do if (![someRecord isKindOfClass:[NSNull class]]). If there aren't too may records then you could log the array to check this, i.e NSLog (@"unsorted: %@", appDelegate.myNonSortedArray); the NSNulls will be printed (null) to the console (or you could examine the array in the debugger by adding a breakpoint at a relevant location. - James Snook
That check doesn't work either because someRecord gets populated with (null) string value if its value in sqlite database is null. If I use [someRecord isEqualToString:@"(null)"] and I log it, I can't see any null records. But still I can't sort it. Maybe I need to use some sqlite function? - user3578142
The ordering method will work on an NSString @"(null)" because that responds to localizedCaseInsensitiveCompare:. It won't work on an NSNull, and that is what it isn't being called on. You may not want @"(null)" in your array but that won't stop it working. Any instance of NSNull needs to be removed from the array appDelegate.myNonSortedArray before you call sortedArrayUsingSelector: on it. If they've all been removed then you won't get this error message. - James Snook

1 Answers

1
votes

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.