【发布时间】:2024-01-23 09:07:01
【问题描述】:
我试图在显示视图之前选择带有谓词的数据库项目并且需要传递一个属性,但我处于 catch-22 情况,因为该属性可能未初始化,产生消息:无法使用实例成员属性初始化程序中的“主题”;属性初始化器在“self”可用之前运行
struct ShowInfo: View
{
@State var subject: Subject
@Environment(\.managedObjectContext) var moc
@FetchRequest(entity: Info.entity(), sortDescriptors: [NSSortDescriptor(key: "title", ascending: true)],
predicate: NSPredicate(format: "subject.title == %@", $subject.title)
) var infos: FetchedResults<Info>
@State var size = UIScreen.main.bounds.width / 3
var body: some View
{
List
{
Text("Info").font(.system(size: 24)).foregroundColor(Color.green)
ForEach(infos, id: \.infoid)
{ info in
ZStack
{
if info.subject == self.subject.title
{
NavigationLink(destination: EditInfoView(info: info))
{
HStack
{
Text(info.title!).frame(width: 150, height: 40).background(Color.blue).foregroundColor(Color.white).cornerRadius(10)
Text(info.value!)
}
}
}
}
}.onDelete(perform: deleteInfo)
}
}
}
【问题讨论】:
-
这能回答你的问题吗? *.com/questions/57871088/…
-
我现在在初始化程序中遇到不同的错误。在显示的示例中, words 变量试图使用在初始化程序中获取的数据进行初始化,但初始化程序抱怨在离开初始化程序之前并未设置所有属性,这是真的。不确定我是否应该在此处或其他回复中发布我的持续问题。