使用Swift 3在Xcode 8中寻找修订后的NSPersistentContainer的清晰教程
我回顾了苹果的:
Xcode 8发行说明:
https://developer.apple.com/library/content/releasenotes/DeveloperTools/RN-Xcode/Introduction.html
从Swift 2.2迁移到Swift 2.3或Swift 3
https://swift.org/migration-guide/
CoreOS 10.12,iOS 10.0,tvOS 10.0和watchOS 3.0中的核心数据的新增function
https://developer.apple.com/library/content/releasenotes/General/WhatNewCoreData2016/ReleaseNotes.html#//apple_ref/doc/uid/TP40017342-CH1-DontLinkElementID_1
还有很多其他的…但是苹果应该提供的一个文档,核心数据编程指南,还没有从Swift 2中更新。
https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/CoreData/FetchingObjects.html#//apple_ref/doc/uid/TP40001075-CH6-SW1
理想情况下,我正在寻找这样的事情,但Swift 3。
https://www.raywenderlich.com/115695/getting-started-with-core-data-tutorial
任何线索将非常感激。
每汤姆的评论(下)我错过了什么步骤?
1)创build一个新的项目“testing”
2)select使用CoreDate(这将创buildTest.xcdatamodeld)
这将自动填充AppDelegate与以下(删除默认注释):
func applicationWillTerminate(_ application: UIApplication) { self.saveContext() } lazy var persistentContainer: NSPersistentContainer = { let container = NSPersistentContainer(name: "Test") container.loadPersistentStores(completionHandler: { (storeDescription, error) in if let error = error as NSError? { fatalError("Unresolved error \(error), \(error.userInfo)") } }) return container }() func saveContext () { let context = persistentContainer.viewContext if context.hasChanges { do { try context.save() } catch { let nserror = error as NSError fatalError("Unresolved error \(nserror), \(nserror.userInfo)") } } }
3)创build实体“Foo”
4)添加属性“bar”types的string
5)下ViewController.swift添加以下(这是从苹果复制,我刚刚取代“…使用”与“打印”)
func findAnimals() { let request: NSFetchRequest<Foo> = Foo.fetchRequest do { let searchResults = try context.fetch(request) print(searchResults) } catch { print("Error with request: \(error)") } }
6)在override func viewDidLoad()下添加findAnimals()。
但是这具体有错误:
- NSFetchRequest <使用未声明的types'NSFetchRequest'
- 上下文<使用未parsing的标识符“上下文”
7)所以你回去添加一些东西在viewController下的函数,使容器可访问(这不是苹果的示例)。
let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
太棒了,我们清除了2错误中的1个,但错误“使用未声明的typesNSFetchRequest”仍然存在。
这里是我卡住的地方。 即使在回顾了所有苹果公布的材料之后,我也找不到一个完整的例子。
@Aaron再次感谢video链接,这让我走上了正轨。 以下是Xcode 8中用Swift 3获取,添加和清除核心数据所需的最低限度内容。
- 新build项目> iOS单一视图应用程序
- 产品名称:“样品”
- 使用核心数据(选中)
- 保存
- 打开Sample.xcdatamodeld
- 添加和实体:“SampleEntity”
- 使用数据模型检查器将Codegen(在Class下)设置为“Class Definition”
- 在新的实体下创build一个属性:“sampleAttribute”
- 打开ViewController.swift
- 在“导入UIKit”下添加“导入CoreData”
-
在类ViewController下添加以下内容:
让context =(UIApplication.shared.delegate as!AppDelegate).persistentContainer.viewContext //从属性获取数据 func getSample(){ 让请求︰NSFetchRequest = SampleEntity.fetchRequest() request.resultType = NSFetchRequestResultType.dictionaryResultType 做{ 让searchResults =将context.fetch(request as!NSFetchRequest <NSFetchRequestResult>)作为! [的NSDictionary] 让searchResultsArray = searchResults.map {$ 0 [“sampleAttribute”] as! 串} print(“searchResultsArray”,searchResultsArray) } catch { 打印(“请求错误:(错误)”) } } //保存到属性 func setSample(){ 让saveSample = SampleEntity(context:context) saveSample.sampleAttribute =“保存一个新的string”。 做{ 尝试context.save() } catch { 打印(“保存错误:\(错误)”) } } //清除属性 func resetSample(){ 让clearSample:NSFetchRequest = SampleEntity.fetchRequest() 让deleteResults = NSBatchDeleteRequest(fetchRequest:clearSample as!NSFetchRequest <NSFetchRequestResult>) 做{ 尝试context.execute(deleteResults) 尝试context.save() } catch { 打印(“保存错误:\(错误)”) } }
-
在重写函数viewDidLoad()下添加以下内容:
的getSample() setSample() 的getSample() resetSample() 的getSample()
-
运行,您将在debugging区域看到以下内容:
searchResultsArray [] //最初,属性是空的 searchResultsArray [“保存新string。”] //现在该属性包含string searchResultsArray [] //此属性已被清除
可能是今年WWDC的video核心数据的新function可以给你更多的内部。
在大约31:20分钟,他显示了一些关于NSFetchRequest
代码。
根据我的理解, NSPersistentContainer
以简单的方式分离主要上下文和工作者上下文。 对于任何UI级别的数据访问(旧的NSMainQueueConcurrencyType),可以简单地调用container.viewContext
,并将container.newBackgroundContext
用于其他数据导入工作(旧的NSPrivtaeQueueConcurrencyType)。 并通过设置automaticallyMergesChangesFromParent
NSManagedObjectContextDidSaveNotification
为true为任何上下文,它等于老监听NSManagedObjectContextDidSaveNotification
。
参考: http : //holko.pl/2016/06/23/core-data/
我仍然不能留下评论。 所以让我把它放在这里。 我相信这两个video会帮助你很多。 Paul Hegarty的“斯坦福大学”课程是最新的!
https://www.youtube.com/watch?v=ssIpdu73p7A – 关于CoreData的讲座https://www.youtube.com/watch?v=whF63GTaW1w – 使用CoreData的演示。
- persistentstorecoordinator sqlite错误代码:522'不是一个错误'
- 在数据库中build模重复事件,并使用UILocalNotification来触发事件
- 保存更新的Core Data实例
- 在Core Data中存储UIColor对象
- 使用核心数据swift ios将实体的所有属性获取到tableview中的行中的标签
- Swift:通过sorting描述符sorting数组
- 访问XCTestCase类中的应用程序委托对象导致misaligned_stack_error_ swift
- 如何计算CoreData对象的唯一date?
- 如何使用选项字典在Swift中closuresCore Data Write-Ahead日志logging?