如何在Xcode 8中使用Swift 3创建managedObjectContext?

编程入门 行业动态 更新时间:2024-10-26 18:16:24
本文介绍了如何在Xcode 8中使用Swift 3创建managedObjectContext?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

面对问题类型AppDelegate的值没有成员managedObjectContext在新的Xcode 8(使用Swift 3,iOS 10)当尝试在View Controller中创建新的上下文

let context =(UIApplication.shared()。delegate as!AppDelegate).managedObjectContext

在Xcode 8中没有AppDelegate.swift文件中的managedObjectContext的代码。AppDelegate.swift中的Core数据栈代码只有:lazy var persistentContainer:NSPersistentContainer property和func saveContext()。没有managedObjectContext属性。

如何使用Xcode 8中的Swift 3创建managedObjectContext)或者没有必要使用Swift 3?

$ b $在Swift3中,您可以通过viewContext访问managedObjectContext

c $ c> let context =(UIApplication.shared.delegate as!AppDelegate).persistentContainer.viewContext

如果在创建项目时已启用Core数据,则此选项可用。但是,对于要包含核心数据的现有项目,请执行添加核心数据的正常过程,并添加以下代码,以便获取

lazy var persistentContainer:NSPersistentContainer = { let container = NSPersistentContainer(name:you_model_file_name) container.loadPersistentStores(completionHandler:{(storeDescription,error )in if let = error { fatalError(未解决的错误\(错误),\(error.userInfo))} } ) return container }()

您需要导入CoreData 。

注意:对于Swift3,ManagedObject子类是自动生成的。 查看更多 WWDC 2016

Facing issue "Value of type 'AppDelegate' has no member 'managedObjectContext' In new Xcode 8 (using Swift 3, iOS 10) when trying to create new context in View Controller

let context = (UIApplication.shared().delegate as! AppDelegate).managedObjectContext

In Xcode 8 there is no code for managedObjectContext inside AppDelegate.swift file. Core Data stack code inside AppDelegate.swift presented only with: lazy var persistentContainer: NSPersistentContainer property and func saveContext () . There is no managedObjectContext property.

How to create managedObjectContext using Swift 3 in Xcode 8) or maybe there is no need to do it using Swift 3 ?

解决方案

In Swift3, you can access the managedObjectContext via the viewContext as

let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext

This option is available if Core data was enabled when creating the project. However, for existing project that you want to include core data, go through the normal process of adding the core data and add the following code which will allow you to get the

lazy var persistentContainer: NSPersistentContainer = { let container = NSPersistentContainer(name: "you_model_file_name") container.loadPersistentStores(completionHandler: { (storeDescription, error) in if let error = error { fatalError("Unresolved error \(error), \(error.userInfo)") } }) return container }()

You will need to import the CoreData.

Note: For Swift3, the ManagedObject Subclass are generated automatically. See more from WWDC 2016

更多推荐

如何在Xcode 8中使用Swift 3创建managedObjectContext?

本文发布于:2023-11-26 21:56:16,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1635351.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何在   Xcode   managedObjectContext   Swift

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!