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/
在macOS 10.12,iOS 10.0,tvOS 10.0和watchOS 3.0https://developer.apple.com/library/content/releasenotes/General/WhatNewCoreData2016/ReleaseNotes.html#//apple_ref/doc/uid/TP40017342-CH1-DontLinkElementID_1中的核心数据有什么新功能
还有很多其他……但是,核心数据编程指南Apple应该提供的一个文档尚未从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)创建一个新项目“测试”
2)选择使用CoreDate(这会创建Test.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)创建实体“Foo”
4)添加属性“bar”类型String
5)在VIEwController.swift下添加以下内容(这是从Apple复制的,我只是将“… use”替换为“print”)
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<使用未声明类型'NSFetchRequest'
> context<使用未解析的标识符“上下文”
7)所以你回过头来向vIEwController下的函数添加一些东西以使容器可访问(这不是示例形式的Apple).
let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.vIEwContext
很好,我们清除了2错误中的1,但错误“使用未声明的类型’NSFetchRequest’”仍然存在.
这就是我被困住的地方.即使在审查了所有Apple发布的材料后,我也找不到完整的例子.
解决方法 @Aaron再次感谢视频链接,让我走上正轨.下面是使用Xcode 8中的Swift 3从核心数据中获取,添加和清除所需的最低限度的快速浏览.>新项目> iOS单一视图应用程序
>产品名称:“样品”
>使用核心数据(已选中)
>保存
>打开Sample.xcdatamodeld
>添加和实体:“SampleEntity”
>使用数据模型检查器将Codegen(在Class下)设置为“Class DeFinition”
>在新实体下创建一个属性:“sampleAttribute”
>打开VIEwController.swift
>在“import UIKit”下添加“import CoreData”
>在Class VIEwController下添加以下内容:
let context = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.vIEwContext// Get data from he attributefunc getSample() { let request: NSFetchRequest = SampleEntity.fetchRequest() request.resultType = NSFetchRequestResultType.dictionaryResultType do { let searchResults = try context.fetch(request as! NSFetchRequest<NSFetchRequestResult>) as! [NSDictionary] let searchResultsArray = searchResults.map {getSample()setSample()getSample()resetSample()getSample()["sampleAttribute"] as! String} print("searchResultsArray",searchResultsArray) } catch { print("Error with request: \(error)") }}// Save to the attributefunc setSample() { let saveSample = SampleEntity(context: context) saveSample.sampleAttribute = "Save a new string." do { try context.save() } catch { print("Error with save: \(error)") }}// clear the attributefunc resetSample() { let clearSample: NSFetchRequest = SampleEntity.fetchRequest() let deleteResults = NSBatchDeleteRequest(fetchRequest: clearSample as! NSFetchRequest<NSFetchRequestResult>) do { try context.execute(deleteResults) try context.save() } catch { print("Error with save: \(error)") }}
>在overrIDe func vIEwDIDLoad()下添加以下内容:
searchResultsArray [] // Initially the attribute is emptysearchResultsArray ["Save new string."] // The attribute Now contains the string searchResultsArray [] // This attribute has been cleared
>运行,您将在调试区域中看到以下内容:
总结以上是内存溢出为你收集整理的ios – 使用Swift 3在Xcode 8中寻找关于修订的NSPersistentContainer的清晰教程全部内容,希望文章能够帮你解决ios – 使用Swift 3在Xcode 8中寻找关于修订的NSPersistentContainer的清晰教程所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)