Cody Harness
Cody Harness

Reputation: 1147

Property 'managedObjectContext' not found on object of type 'AppDelegate *'

I am updating some code in Objective-C and need to convert from Swift 3 to a newer version. I am running into an error that I'm not sure what to do about. The code with error is

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

The managedObjectContext portion of the AppDelegate is

lazy var managedObjectContext: NSManagedObjectContext = {
    // Returns the managed object context for the application (which is already bound to the persistent store coordinator for the application.) This property is optional since there are legitimate error conditions that could cause the creation of the context to fail.
    let coordinator = self.persistentStoreCoordinator
    var managedObjectContext = NSManagedObjectContext(concurrencyType: .mainQueueConcurrencyType)
    managedObjectContext.persistentStoreCoordinator = coordinator
    return managedObjectContext
}()

The error that I get says Property 'managedObjectContext' not found on object of type 'AppDelegate *'. I don't know Objective-C, I just needed to update some resource files and the rest of the app falls into place around them, so I really don't know how I can resolve this. Any tips on what to do? This line is repeated several times throughout the app.

Upvotes: 1

Views: 102

Answers (1)

Asperi
Asperi

Reputation: 257709

If your bridging headers are set correctly, then it is enough to make property @objc available as below

@objc lazy var managedObjectContext: NSManagedObjectContext = {
    // Returns the managed object context for the application (which is already bound to the persistent store coordinator for the application.) This property is optional since there are legitimate error conditions that could cause the creation of the context to fail.
    let coordinator = self.persistentStoreCoordinator
    var managedObjectContext = NSManagedObjectContext(concurrencyType: .mainQueueConcurrencyType)
    managedObjectContext.persistentStoreCoordinator = coordinator
    return managedObjectContext
}()

Upvotes: 1

Related Questions