/ is a forward slash.
Where does the error happen? Which line?
What is cards?
Also, char is a Character, and you're trying to compare one Character against an array of Character. Do you mean to use something like if (!allowed.contains(char)) {?
From what I can gather you're trying to make sure the value in the field is a valid latitude? There are better functions to do this.
You should create a Set of Character. From the documentation: You use a set instead of an array when you need to test efficiently for membership and you aren’t concerned with the order of the elements in the collection, or when you need to ensure that each element appears only once in a collection.
So, add this somewhere once only; you don't need to re-create this Set everywhere:
let validCoordCharacters: Set<Character> = ["-", ".", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9"]
You're currently stripping out unwanted characters when the TextField first appears, but since it's an editable TextField you should be doing this when the value changes, too:
.onChange(of: theTextFieldText) {
theTextFieldText.removeAll(where: { !validCoordCharacters.contains($0) } )
}
And you will need to ensure that the minus character - is only ever at the beginning of the String for it to be a valid latitude/longitude. You could do this by checking if the String contains that character, and if its location in the String is not location 0, then you remove it from the String and add it to the beginning.
Topic:
UI Frameworks
SubTopic:
SwiftUI
Tags: