Unrecognized selector when setting NSArray property

Total Post:27

 1267  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?

  1. Post:214

    Re: Unrecognized selector when setting NSArray property

    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]];

      Modified On Apr-10-2018 01:55:47 AM