gpt4 book ai didi

SwiftUI: ForEach of FetchedResults 给出错误 - "Value of type ' NSManagedObject' has no member ..."

转载 作者:行者123 更新时间:2023-12-01 11:12:01 24 4
gpt4 key购买 nike

我是 SwiftUI 的新手,部分是 Swift,所以希望有人能对我明显缺少的东西有所了解。原谅乱码!我还没有机会重构。

我正在使用 @FetchRequest属性包装器拉出所有列表 MemoriesCoreData 回来模型,带 NSFetchRequest<Memory>在其上定义的方法,从 NSManagedObject 扩展.

当我遍历 FetchRequest<Memory> 的结果时在 SwiftUI 中通过 ForEach ,它返回一个 NSManagedObject 的实例进入循环变量而不是 Memory ,从而显示错误:Value of type 'NSManagedObject' has no member 'createdAt'对于以下 SwiftUI 类:

import SwiftUI

struct MemoryView: View {

@Environment(
\.managedObjectContext
) var managedObjectContext

@FetchRequest(
fetchRequest: Memory.allMemories()
) var allMemories: FetchedResults<Memory>

@State private var newMemory = ""

var body: some View {
NavigationView {
List {
Section(header: Text("What's on your mind?")) {
HStack {
TextField("New memory", text: self.$newMemory)

Button(action: {
let memory = Memory(context: self.managedObjectContext)

memory.title = self.newMemory
memory.createdAt = Date()

do {
try self.managedObjectContext.save()
} catch {
print(error)
}

self.newMemory = ""
}) {
Image(systemName: "plus.circle.fill")
.foregroundColor(.green)
.imageScale(.large)
}
}
}
.font(.headline)

Section(header: Text("My Memories")) {
ForEach(self.allMemories) { memoryItem in
MemoryRow(title: memoryItem.title, createdAt: memoryItem.createdAt)
// /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\ /\
// >>> Error:
// >>> Value of type 'NSManagedObject' has no member 'createdAt'
}
}
}
.navigationBarTitle(Text("Memories"))
.navigationBarItems(trailing: EditButton())
}
}
}

struct MemoryView_Previews: PreviewProvider {
static var previews: some View {
MemoryView()
}
}

供引用, Memory有问题的模型如下:

import Foundation
import CoreData
import CoreLocation

// Define the CoreData Object Model.
// Ensuring that this class name matches
// the entity model name.
public class Memory: NSManagedObject, Identifiable {
@NSManaged public var title: String?
@NSManaged public var body: String?

@NSManaged public var latitude: Double
@NSManaged public var longitude: Double

@NSManaged public var createdAt: Date?
@NSManaged public var updatedAt: Date?

// @NSManaged public var type: Type?
// @NSManaged public var tags: [Tags]?
}

// Create an extension in which to place things out of the context
// of the initial class declaration within.
extension Memory {
// Define a helper method for accessing a generated
// CLLocationCoordinate2D instance from the objects
// latitude and longitude values.
var locationCoordinate: CLLocationCoordinate2D {
CLLocationCoordinate2D(
latitude: latitude,
longitude: longitude
)
}

// Define a helper method that grabs all instances of a Memory
// sorted by their created date in ascending order.
static func allMemories() -> NSFetchRequest<Memory> {
let request: NSFetchRequest<Memory> = Memory.fetchRequest() as! NSFetchRequest<Memory>

let sortDescriptor = NSSortDescriptor(key: "createdAt", ascending: true)

request.sortDescriptors = [sortDescriptor]

return request
}
}

对此的任何帮助将不胜感激,因为我完全没有想法。我认为明天的问题有一个新的头脑!

编辑:这是 MemoryRow类(class):

import SwiftUI

struct MemoryRow: View {
var title: String = ""
var createdAt: String = ""

var body: some View {
HStack {
VStack(alignment: .leading) {
Text(title)
.font(.headline)

Text(createdAt)
.font(.caption)
}
}
}
}

struct MemoryRow_Previews: PreviewProvider {
static var previews: some View {
MemoryRow(title: "Test Title", createdAt: "Test Date")
}
}

最佳答案

就我而言,这是因为 optional当我们在数据模型中添加属性时默认选择的数据类型,因此在 NSManagedObjectClass它看起来像这样@NSManaged public var name: String?当然,要解决这个问题,可以删除 ?从上面一行或使用 ??使用该属性时的 nil 合并运算符,例如 ClassName.name ?? "Unknown"根据给出的答案和评论,我在类里面检查了我的属性名称是否有任何错字,然后我意识到了这个问题。现在必须处理 SwiftUI 的这种行为它不会显示实际错误或将显示在任何其他行上。🤯
希望这可以帮助像我这样的人。

关于SwiftUI: ForEach of FetchedResults 给出错误 - "Value of type ' NSManagedObject' has no member ...",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59061270/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com