Home > DeveloperSection > Forums > Unrecognized selector when setting NSArray property
Glen Martin
Glen Martin

Total Post:27

Posted on    July-28-2015 11:18 PM

 iPhone iPhone  Ios  Objective C 

 1 Reply(s)
 550  View(s)
Rate this:

I have a method that returns an array:

-(NSArray*)fetchStoresFromContext {

     NSManagedObjectContext *context = [RKManagedObjectStore


    NSFetchRequest *fetchRequest = [NSFetchRequest

fetchRequestWithEntityName: @"Store"];

    NSSortDescriptor *descriptor = [NSSortDescriptor sortDescriptorWithKey:

@"name" ascending:YES];

    fetchRequest.sortDescriptors = @[descriptor];

    NSError *error = nil;

    NSArray *fetchedObjects = [context executeFetchRequest:fetchRequest error:


    return fetchedObjects;


If create a new NSArray and call this method like this: 

NSArray *test = [self fetchStoresFromContext]; 

everything works fine. [test count] returns 6. 

I have the following property: 

@property (nonatomic, strong) NSArray* stores; 

if I call _stores = [self fetchStoresFromContext] I get the following error: 

-[__NSCFNumber length]: unrecognized selector sent to instance 0x15e3c2f0 


What's going on here? The property is an NSArray and the object is an NSArray as well so why am I having this problem?

Tarun Kumar

Total Post:210

Posted on    July-29-2015 8:04 AM

The error means that your code is passing an NSNumber where the called code expects an NSString or some other object that has a length method.

Not clear enough...Is the error thrown when you do [_stores count] ?

If so, try to instantiate your array before counting :

_stores = [[NSArray alloc] initWithArray:[self fetchStoresFromContext]];

Don't want to miss updates? Please click the below button!

Follow MindStick