user1107173
user1107173

Reputation: 10764

CoreData: ObjectID Error: "No known class method for selector 'managedObjectContext'

@implementation FirstScene
...
- (void)nextScene {
    Meetings *meetings = (Meetings *) [NSEntityDescription insertNewObjectForEntityForName:@"Meetings" inManagedObjectContext:self.managedObjectContext];
    NSManagedObjectID* objectID = [meetings objectID];
    [secondScene setObjectID:objectID];
}
...
@end

@implementation SecondScene
....
- (void)viewDidLoad
{
    [super viewDidLoad];
    self.managedObjectContext = [(STAppDelegate *)[[UIApplication sharedApplication]  delegate] managedObjectContext];
}

+ (void)setObjectID:(NSManagedObjectID*)objectID {
    NSManagedObjectContext *context = [self managedObjectContext]; // ERROR
    Meetings *theSameMeetings = (Meetings *)[context objectWithID:objectID];
}
...
@end

I'm getting an error "No known class method for selector 'managedObjectContext' in the following code:

NSManagedObjectContext *context = [self managedObjectContext];

My objective here is to pass the objectID of *meetings (managedObjectContext) in FirstScene to the SecondScene so that I can continue to add to the entity *meetings attributes. Thanks.

Upvotes: 1

Views: 1061

Answers (3)

Tom Harrington
Tom Harrington

Reputation: 70956

That's not a Core Data error, it's a basic Objective-C error. You're in this method:

+ (void)setObjectID:(NSManagedObjectID*)objectID;

The "+" says that this is a class method, not an instance method. So self in this case is the SecondScene class, not any particular instance of that class. When you try to do this:

NSManagedObjectContext *context = [self managedObjectContext];

...you're trying to call a method named +managedObjectContext, i.e. a class method with that name. That apparently doesn't exist, which isn't too surprising.

I suspect you intended for setObjectID: to be an instance method, which means it should have a - instead of a + at the start of the line.

Upvotes: 2

JeffN
JeffN

Reputation: 1605

By calling [self managedObjectContext] the compiler is looking for a method name in the current target that is -(void)managedObjectContext which clearly doesn't exist. The best way to pass an object ID is set a property and synthesize that property, that will allow you to assign the objectID to an instance of meeting, and then retrieve it from said instance.

--EDIT--

In whichever class you need to access your ManagedObjectID you want to declare your delegate as such:

AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];

Then in your setObjectID method you can call:

 NSManagedObjectContext *context = [appDelegate managedObjectContext];

Upvotes: 0

Reno Jones
Reno Jones

Reputation: 1979

Brother, as it says you do not have 'managedObjectContext' method in your 'SecondScene'. is it?

Apparently you are a beginner to core data and you have just stepped into it.

however, Go to AppDelegate of your project, and search for '-(void)managedObjectContext' and you will see that there is a manageobjectcontext method there, replace [self manageobjectcontext] with the method from AppDelegate.

This will allow you to have access to appdelegate

YourAppDelegateClass   *appDelegate = (YourAppDelegate *)[[UIApplication sharedApplication] delegate]; and now call [appDelegate managedObjectContext];        

Hope this helps.

Upvotes: 0

Related Questions