[英]Swift: XCode6 Beta 5 giving errors on core data objects in AppDelegate
[英]How to transform array of core data managed objects into an “identifiable” list, in swift? (Xcode 11, Beta 5)
如何將使用Swift / IOS通過“ fetchRequest”從“核心數據”中檢索到的管理對象數組轉移到“可識別”數組中?
示例-如何使“任務”數組“可識別”
let fetchRequest : NSFetchRequest<Todo> = Todo.fetchRequest()
let tasks = try context?.fetch(fetchRequest)
背景:
ForEach(Data, id: \\.idAttribute)
identified(by: .self)
的功能已被新語法替換:
ForEach(filteredGrapes, id: \.id) { grape in
GrapeCell(grape: grape)
}
使用名為ItemStore.xcdatamodeld
的文件,該文件的實體ItemDAO
定義為啟用了Generation=Class Definition
並具有名為title
的字符串屬性。
注意:必須先獲得Product/Clear Build Folder
,然后重新啟動Xcode,以使Xcode11Beta5找到正確的密鑰路徑,這似乎是Xcode11Beta5中的錯誤。
import Foundation
import SwiftUI
import CoreData
class MyItemStore {
public static func defaultItems() -> [ItemDAO]{
let store = NSPersistentContainer(name: "ItemStore")
store.loadPersistentStores { (desc, err) in
if let err = err {
fatalError("core data error: \(err)")
}
}
let context = store.viewContext
let item = ItemDAO(context: context)
item.title = "hello you"
try! context.save()
return [
item,
item,
]
}
}
struct CoreDataView: View {
let items: [ItemDAO] = MyItemStore.defaultItems()
var body: some View {
VStack{
ForEach(items, id: \.title) { (item: ItemDAO) in
Text(item.title ?? "no title")
}
Text("hi")
}
}
}
extension Todo: Identifiable {
public var id: String {
//return self.objectID.uriRepresentation().absoluteString
return self.title!
}
}
Identifiable
在那里您可以照常添加Identifiable
,但這不是必需的,盡管擴展名也可以添加Identifiable。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.