I want to decouple some views. For this case I created an initializer for a view with a custom argument in an extension in a second file. Unfortunately I get this message: "'self' used before all stored properties are initialized".
If I put view and extension in the same file, it works. But in this case, the view file needs to know the custom argument. This may not be needed in another project.
For simplicity, I have created a sample code that shows the problem. How can I initialize the @State property name in the extension initializer if the extension is in another file?
// ListCell.swift
import SwiftUI
struct ListCell: View {
@State var name: String
var count: Int
var body: some View {
HStack {
Text(name)
Text(": \(count)")
}
}
}
// ListCell+Ext.swift
import SwiftUI
extension ListCell {
init(name: String) {
self.name = name
count = 0
}
}