I am making a section with TextFields and Button("Continue") and then use .disable(isValidAddress) modifier to a whole section to disable the button. The code works well, but I am seeking any solution to make it more succinct with no need to write .hasPrefix() or .hasSuffix() to all parameters one by one.
var isValidAddress: Bool {
if name.hasPrefix(" ") || street.hasPrefix(" ") || city.hasPrefix(" ") || country.hasPrefix(" ") {
return false
} else if name.hasSuffix(" ") || street.hasSuffix(" ") || city.hasSuffix(" ") || country.hasSuffix(" ") {
return false
}
return true
}
3
Answers
}
You can add them to an array and trim white space and check if empty in a loop
I used a function here but a computed property works just as well.
If you don’t mind moving some complexity into extensions, you can tidy up the call site. This makes it more readable, and less error prone – you can easily add in another field without mistakes for example.
If you do mean none of them are "all whitespace strings" then:
One benefit of having named functions for these things is you can test the pieces, so you can write a test to ensure that
isNotAllWhitespace
works the way you expect. You couldn’t if the logic likename.hasPrefix(" ") || street.hasPrefix(" ")
is mixed in with your isValidAddress function.