0

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.

6
  • 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!) Commented Apr 27, 2014 at 11:54
  • 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? Commented Apr 27, 2014 at 12:27
  • 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. Commented Apr 27, 2014 at 12:39
  • 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? Commented Apr 27, 2014 at 12:48
  • 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. Commented Apr 27, 2014 at 13:00

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Yes, and I have removed all NSNull objects by using [appDelegate.myUnsortedArray removeObjectIdenticalTo:[NSNull null]];,and sortedArrayUsingSelector: now works.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.