Swiftui 如何初始化@Binding数组
因此,我正在进行一些重构,我遇到了我想要重构的这行代码:Swiftui 如何初始化@Binding数组,swiftui,Swiftui,因此,我正在进行一些重构,我遇到了我想要重构的这行代码: struct MyView: View { @State private var myArrayOfCustomObjects = [CustomObject] let text: String var body: some View { Text(text) } } 然后,当我想重构视图时 struct ExtractedView: View {
struct MyView: View {
@State private var myArrayOfCustomObjects = [CustomObject]
let text: String
var body: some View {
Text(text)
}
}
然后,当我想重构视图时
struct ExtractedView: View {
@Binding var customObjects: [CustomObject]
let text: String
init(customObjects: Binding<Array<CustomObject>>, text: String) {
self.customObjects = customObjects // Error: 'self' used before all stored properties are initialized
// Also tried _customObjects = customObjects
self.text = text
}
var body: some View {
Text(text)
}
}
struct ExtractedView:View{
@绑定变量customObjects:[CustomObject]
让文本:字符串
init(自定义对象:绑定,文本:字符串){
self.customObjects=customObjects//错误:在初始化所有存储的属性之前使用了“self”
//还尝试了_customObjects=customObjects
self.text=文本
}
var body:一些观点{
文本(文本)
}
}
这段代码当然被简化了,但我担心由于示例中没有公开的复杂性,我可能会遇到这个错误。欢迎任何反馈
我做错了什么
(我还有一个
环境
实例(managedObjectContext)和一个coreData类-它在初始化的init内部有一些逻辑,但我认为它与这个代码示例无关)这将起作用!另外,请尝试清理生成文件夹并首先生成项目
struct ExtractedView: View {
@Binding var customObjects: [CustomObject]
let text: String
init(customObjects: Binding<Array<CustomObject>>, text: String) {
self._customObjects = customObjects
self.text = text
}
var body: some View {
Text(text)
}
}
struct CustomObject { }
struct ExtractedView:View{
@绑定变量customObjects:[CustomObject]
让文本:字符串
init(自定义对象:绑定,文本:字符串){
self.\u customObjects=customObjects
self.text=文本
}
var body:一些观点{
文本(文本)
}
}
结构CustomObject{}