从AppDelegate更改另一个文件中的变量

时间:2017-04-21 04:39:43

标签: ios swift xcode swift3 appdelegate

背景

我正在尝试从managedObjectContext访问ProjectsController.swift中的变量AppDelegate.swift

代码

ProjectsController.swift

var managedObjectContext: NSManagedObjectContext? = nil

var fetchedResultsController: NSFetchedResultsController<Project> {
    if _fetchedResultsController != nil {
        return _fetchedResultsController!
    }

    let fetchRequest: NSFetchRequest<Project> = Project.fetchRequest()

    // Set the batch size to a suitable number.
    fetchRequest.fetchBatchSize = 20

    // Edit the sort key as appropriate.
    let sortDescriptor = NSSortDescriptor(key: "name", ascending: false)

    fetchRequest.sortDescriptors = [sortDescriptor]




    //The output is "nil"
    print(self.managedObjectContext as Any)

    //Couldn't unwrap optional value (self.managedContext!)
    let aFetchedResultsController = NSFetchedResultsController(fetchRequest: fetchRequest, managedObjectContext: self.managedObjectContext!, sectionNameKeyPath: nil, cacheName: "Master")





    aFetchedResultsController.delegate = self
    _fetchedResultsController = aFetchedResultsController

    do {
        try _fetchedResultsController!.performFetch()
    } catch {
        // Replace this implementation with code to handle the error appropriately.
        // fatalError() causes the application to generate a crash log and terminate. You should not use this function in a shipping application, although it may be useful during development.
        let nserror = error as NSError
        fatalError("Unresolved error \(nserror), \(nserror.userInfo)")
    }

    return _fetchedResultsController!
}

AppDelegate.swift

let projectsViewController = ProjectsViewController()

func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    // Override point for customization after application launch.


    //The output prints both of the print statements, so it has executed the change in variable.  The viewContext also outputs as it is supposed to.
    print(self.persistentContainer.viewContext)
    //Here is where the variable changes
    projectsViewController.managedObjectContext = self.persistentContainer.viewContext
    print(true)
    return true
}

问题

我只是想知道为什么,即使我从AppDelegate.swift更新了变量,ProjectsController.swift也无法解包该值,因为它仍然是nil。连接是否有问题,或者可能有更好的方法来访问AppDelegate.swift的不同文件中的变量。

额外信息:

  • ProjectsController位于导航视图控制器
  • 我使用的是Swift 3

我想不出任何其他信息,我是Swift的初学者,所以如果您需要额外的信息来解决这个问题,或者如果它无法解决,请告诉我。

感谢您的时间和帮助!

1 个答案:

答案 0 :(得分:0)

如果在设置managedObjectContext之前使用fetchedResultsController,则会发生这种情况。例如如果视图控制器在加载数据后尝试获取数据,并且您有机会在设置managedObjectContext的应用程序委托中运行代码。

建议您尝试将ProjectsViewController的创建与其依赖项一起保存,这样它就永远不会处于不可用状态。例如,在您的app delegate中:

let projectsViewController: ProjectsViewController = {
    let vc = ProjectsViewController()
    vc.managedObjectContext = self.persistentContainer.viewContext
    return vc
}()

此代码将projectsViewController声明为立即执行的闭包(实际上是函数)的值。