Avatar ·

SwiftUI: How to implement custom init with @Binding variables

Hello! I'm new to SwiftUI and trying to create a custom view with a text input field. I need the parent view to be able to pass state management through @Binding. But when I try to add a custom init, Xcode complains that the $text property is not initialized. Here's my code:

\n\n
struct CustomTextField: View {\n    @Binding var text: String\n    var placeholder: String\n    \n    // Trying to make a custom init\n    init(placeholder: String, text: Binding<String>) {\n        self.placeholder = placeholder\n        self.text = text // Error: 'self' used before all stored properties are initialized\n    }\n    \n    var body: some View {\n        TextField(placeholder, text: $text)\n            .textField