Unrecognized selector when setting NSArray property

时间:2015-07-28 16:42:01

标签: ios objective-c

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 个答案:

答案 0 :(得分:0)

错误意味着您的代码正在传递NSNumber,其中被调用的代码需要NSString或其他具有length方法的对象。

不够清楚......当你做[_stores count]时会抛出错误吗?

如果是这样,请在计算之前尝试实例化数组:

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