Свойство managedObjectContext не найдено в объекте типа AppDelegate * - PullRequest
1 голос
/ 26 апреля 2020

Я обновляю некоторый код в Objective- C и мне нужно конвертировать из Swift 3 в более новую версию. Я сталкиваюсь с ошибкой, что я не уверен, что делать. Код с ошибкой:

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

Управляемая часть AppDelegate - управляемого элемента управления:

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
}()

Я получаю сообщение об ошибке Property 'managedObjectContext' not found on object of type 'AppDelegate *'. Я не знаю Objective- C, мне просто нужно было обновить некоторые файлы ресурсов, и остальная часть приложения стала на свои места, так что я действительно не знаю, как мне это решить. Любые советы о том, что делать? Эта строка повторяется несколько раз по всему приложению.

1 Ответ

1 голос
/ 26 апреля 2020

Если ваши мостовые заголовки установлены правильно, то достаточно сделать свойство @objc доступным, как показано ниже

@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
}()
...