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”
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发布的材料后,我也找不到完整的例子.
解决方法
>新项目> 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 attribute func 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 { $0["sampleAttribute"] as! String} print("searchResultsArray",searchResultsArray) } catch { print("Error with request: \(error)") } } // Save to the attribute func setSample() { let saveSample = SampleEntity(context: context) saveSample.sampleAttribute = "Save a new string." do { try context.save() } catch { print("Error with save: \(error)") } } // clear the attribute func 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()下添加以下内容:
getSample() setSample() getSample() resetSample() getSample()
>运行,您将在调试区域中看到以下内容:
searchResultsArray [] // Initially the attribute is empty searchResultsArray ["Save new string."] // The attribute now contains the string searchResultsArray [] // This attribute has been cleared