1

I am working with cocos2d and CoreData. I have imported "AppDelegate.h" but I can't create an object from it. I would think that all I would do is:

AppDelegate *delegate;

When I do this it get an error saying AppDelegate is not defined. The AppDelegate .h and .m files are next to the main file.

Also, when I try to write to a file I don't get an error but it does not write.

2
  • 1
    Well, don't confuse your managedObjectContext with your app delegate. Given that you are using core data, you are probably looking to create an NSManagedObjectContext *context; line instead of what you've written above. Mar 19, 2013 at 23:21
  • Oh yes you are right, however i need to be able to create an object from the AppDelegate to use coredata. Correct me if im wrong. Mar 19, 2013 at 23:42

2 Answers 2

0

The boiler plate code created by XCode (I'm assuming you are using XCode) will create an NSManagedObjectContext for you called *managedObjectContext. If you need to create other objects that are going to interact with your core data model (such as a view controller), you simply pass in that managedObjectContext object as an argument (or link it to a property in your custom class) and interact with it in your custom class. It's worth noting that it's not good practice to be passing around an AppDelegate object in your app. Your app delegate should be at the foundation of your code base and not treated as a typical class. There are definitely times when you will want (or need) to pass the app delegate as an object or reference it in IB, but typically your app will launch in your

- (void)applicationDidFinishLaunching:(NSNotification *)aNotification

method and you will launch your primary view controller from there.

Some things in core data also require the persistent store coordinator (such as retrieving managed object ID's), so you may need to pass that in as well. If you need some help working with Core Data, there are a bunch of great articles and books on the subject. I recommend reading as many as you can, since Core Data can be difficult to comprehend at first. It helped me to read a bunch of tutorials at first since everyone explains it a little differently.

Here's a great recent tutorial written on Bindings, Core Data, and working with the app delegate to get you started: http://www.raywenderlich.com/21752/how-to-use-cocoa-bindings-and-core-data-in-a-mac-app

0

In Cocos2d 2.0, AppDelegate is renamed to AppController

AppController *app = (AppController*)[UIApplication sharedApplication].delegate;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.