1

I am a beginner programmer and I am targeting OSX. I want to create a reference to the managed object context property thats inside AppDelegate, to use it in a Core Data project I'm creating

I try

let appDelegate = UIApplication.sharedApplication().delegate as AppDelegate
let aVariable = appDelegate.someVariable

or make it mutable, typing var instead of let. Can anyone give me some help, please?

Thanks

2
  • I haven't tried this but I believe delegate are optional so you should unwrap it before use: UIApplication.sharedApplication()?.delegate as AppDelegate. Commented Jul 15, 2014 at 9:51
  • @Greg Thanks a lot but I always get the same message "myController.type (the class I have created where I'm placing my controls) does not have a member named 'myAppDelegate' (the instance of AppDelegate that supposedly I have created) Commented Jul 15, 2014 at 13:36

2 Answers 2

6

For macos apps you have to use NSApplication instead of UIApplication:

let appDelegate = NSApplication.sharedApplication().delegate as AppDelegate

I presume you are using the Cocoa Application template in Xcode, and you have enabled the Use Core Data checkbox - in that case NSApplication will be generated with a managedObjectContext property, defined as follows:

var managedObjectContext: NSManagedObjectContext? {
    ...
}

That's a computed property, returning an optional, so you have to unwrap it before using.

Sign up to request clarification or add additional context in comments.

9 Comments

Thanks a lot for your help but I made a mistake, I pasted a code which I was not using. Actually I'm using the same code you use, but in the next step, when I want to use the appDelegate constant, nothing appears to me. I type appDelegate. and autocomplete does gives me the option to choose the managedObjectContext property
What do you mean by 'nothing appears'? If it's a visual problem, then it is probably not related to what you are describing in your question. Please provide more info about what is the problem you're having, modifying your question as needed
@Alonso thanks a lot but, I create an new Project and check the Core Data check box. And after that I create a New Swift Class that is a sub Class of NSObject. In the first line of code (inside a class) I want to create a constant that references the AppDelegate Class and there my problems start. The error message I get is "myController.type (the class I have created where I'm placing my controls) does not have a member named 'myAppDelegate' (the instance of AppDelegate that supposedly I have created)
I presume it's a class property, not a variable defined in a function. How/where do you instantiate that class?
You are right!!!! I works only when I call them inside a function (awakeFromNib) , I cannot call them or create properties outside a function? Why is that????
|
1

The problem is that you're trying to use code from the iOS frameworks in the OS X application. You need NSApplication, UIApplication's OS X counterpart.

let appDelegate = NSApplication.sharedApplication().delegate as AppDelegate
let aVariable = appDelegate.someVariable

1 Comment

Thanks but I typed to Antonio what the problem is. I simply cannot access instance of AppDelegate (appDelegate) I have created. You are right in your code. But it does not works for me. Thanks

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.