UNRECOGNIZED SELECTOR WHEN SETTING NSARRAY PROPERTY

Glen Martin

Total Post:27

Points:191
Posted by  Glen Martin
 892  View(s)
Ratings:
Rate this:

I have a method that returns an array:

-(NSArray*)fetchStoresFromContext {

     NSManagedObjectContext *context = [RKManagedObjectStore

                                                        defaultStore].mainQueueManagedObjectContext;

    NSFetchRequest *fetchRequest = [NSFetchRequest

fetchRequestWithEntityName: @"Store"];

    NSSortDescriptor *descriptor = [NSSortDescriptor sortDescriptorWithKey:

@"name" ascending:YES];

    fetchRequest.sortDescriptors = @[descriptor];

    NSError *error = nil;

    NSArray *fetchedObjects = [context executeFetchRequest:fetchRequest error:

&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. Tarun Kumar

    Post:214

    Points:1642
    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]];

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!