I have a SwiftUI view that takes user name in a String declared as a State property. How can I add an additional attribute to the same variable?
struct Login: View {
@Trimmed(characterSet: .whitespaces)
@State private var userName: String = ""
var body: some View {
VStack {
TextEditor(text: $userName)
}
}
}
In the code above, I am trying to add a property wrapper Trimmed
that will automatically keep the user input string clean.
I am getting an error saying Cannot convert value of type 'State<String>' to expected argument type 'String'
Is there a way out here?
Update:
Here is the code for the property wrapper
@propertyWrapper
struct Trimmed {
private var value: String!
private let characterSet: CharacterSet
var wrappedValue: String {
get { value }
set { value = newValue.trimmingCharacters(in: characterSet) }
}
init(wrappedValue: String) {
self.characterSet = .whitespacesAndNewlines
self.wrappedValue = wrappedValue
}
init(wrappedValue: String, characterSet: CharacterSet) {
self.characterSet = characterSet
self.wrappedValue = wrappedValue
}
}