简体   繁体   English

SwiftUI:如何将核心数据对象传递给视图?

[英]SwiftUI: How to pass core data objects to views?

What I want to do: pass a reference to a NSManagedObject of a FetchRequest (here from: TestView) to another Child-View (here: LogRectangle).我想要做的:将对 FetchRequest 的 NSManagedObject 的引用(此处来自:TestView)传递给另一个子视图(此处:LogRectangle)。

What I tried: These are basically the important lines :我尝试了什么:这些基本上是重要的行

List(testObjects, id: \.self) { habit in
    LogRectangle(testObject: testObject)
}

And this is the whole code:这是整个代码:

struct TestView : View {
    @Environment(\.managedObjectContext) var managedObjectContext
    @FetchRequest(
        entity: TestObject.entity(),
        sortDescriptors: [NSSortDescriptor(keyPath: \TestObject.name, ascending: true)]
    ) var testObjects: FetchedResults<TestObject>


    var body: some View {
        NavigationView {
            HStack {
                Button(action: {
                    let testObject = TestObject(context: self.managedObjectContext)
                    habit.name = "TestObject String"
                    do {
                        try self.managedObjectContext.save()
                    } catch {
                        // handle the Core Data error
                    }
                }) {
                    Text("Insert example TestObject")
                }
                List(testObjects, id: \.self) { habit in
                    LogRectangle(testObject: testObject)
                }
            }
                .navigationBarTitle("Test")
        }
    }
}

struct LogRectangle : View {
    var habit : TestObject

    var body: some View {
        Text(habit.name)
            .font(.title)
            .foregroundColor(.white)        
    }
}

Here I get this error on the line with the text in the class LogRectangle.在这里,我在 class LogRectangle 中的文本行中收到此错误。

Cannot convert value of type 'String?' to expected argument type 'LocalizedStringKey'

You are encountering this error perhaps because the attribute, name , is defined as an optional string in the data model.您遇到此错误可能是因为属性name被定义为数据 model 中的可选字符串。

The Text struct does not take an optional value. Text结构不采用可选值。 It has to be an explicit value that is:它必须是一个明确的值,即:

  • A value conforming to the StringProtocol符合StringProtocol的值
  • A literal string that can be used for loading a localized string可用于加载本地化字符串的文字字符串

文本结构的参数

Therefore, you will need to make some changes to the model that you are passing to the LogRectangle .因此,您需要对传递给LogRectangle的 model 进行一些更改。 These change can either be made at the data model level (and then updating the associated NSManagedObject class, or you can have a computed property on the model to provide a non-optional value of the attribute.这些更改可以在数据 model 级别进行(然后更新关联的NSManagedObject class,或者您可以在 model 上拥有一个计算属性以提供该属性的非可选值。

You may have better luck using the testObject indices for the list rather than iterating on the actual objects.使用列表的 testObject 索引而不是迭代实际对象可能会有更好的运气。 Try this:尝试这个:

List(testObjects.indices, id: \.self) { index in
     LogRectangle(testObject: testObjects[index])
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM