BBruce
BBruce

Reputation: 265

iOS Core Data managedObjectContext: No know class method for selector

Probably something simple, but none of my searches are getting me what I need.

I am using Core Data in my app to manage players and matches for a game. My core data stack is completely generic and in my AppDelegate. I am trying to make a helper class that will return an Entity (Player) based on a search string. When I try to wire up core data, I am getting this error when setting up my fetchRequest.

No known class method for selector 'managedObjectContext'

here's the code... PlayerSearch.h

#import <Foundation/Foundation.h>
#import "Player.h"

@interface PlayerSearch : NSObject

+ (Player *)searchForPlayerWithID:(NSString*)playerID;

@end

PlayerSearch.m

import "PlayerSearch.h"

#import "AppDelegate.h"

@interface PlayerSearch()
@property(nonatomic, strong) NSManagedObjectContext *managedObjectContext;

@end

@implementation PlayerSearch

-(NSManagedObjectContext*)managedObjectContext{
return [(AppDelegate*)[[UIApplication sharedApplication]delegate]managedObjectContext];
}

+ (Player *)searchForPlayerWithID:(NSString*)playerID
{

NSFetchRequest *fetchRequest = [[NSFetchRequest alloc] init];
fetchRequest.entity = [NSEntityDescription entityForName:@"Player" inManagedObjectContext:[self managedObjectContext]];

...

return player;
}
@end

The error is showing up when I set the fetchRequest.entity's context. This is exactly how I am setting the context in my view controllers throughout the app and it works fine there...

What am I missing?

Upvotes: 1

Views: 1046

Answers (1)

TylerP
TylerP

Reputation: 9829

Your searchForPlayerWithID: method is a class method and you are trying to call an instance method (managedObjectContext) on self from within that class method.

You can fix this by changing your managedObjectContext method to be a class method:

+ (NSManagedObjectContext *)managedObjectContext
{
    return [(AppDelegate *)[[UIApplication sharedApplication] delegate] managedObjectContext];
}

Upvotes: 3

Related Questions