Checking the contents of a TextField variable method not working

Hoping someone can help me with this… The error is… Generic parameter ‘/‘ cannot be inferred.

                            .multilineTextAlignment(.center)
                            .onAppear(perform: {
                                var checkFirstCardLatitude = cards.firstCardLatitude
                                let charArray = Array(checkFirstCardLatitude)
                                let allowed: [Character] = ["-", ".", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9"]
                                for char in charArray {
                                    if char != allowed {
                                        cards.firstCardLatitude = "000.000000" // Reset Text Field
                                    }
                                }
                            })
Answered by darkpaw in 838112022

There is a slight issue with the regex, in that it seems to be allowing you to enter something like -12..sssds. It seems that as soon as you enter the second full-stop you can enter anything, so you may want to either check the regex, or work around it with some more code in the Coordinate.validate() function.

Thanks again for your help with this. Although I have not used your solution exactly it did help me a great deal to construct a solution (example enclosed).

  • Still working on these examples of input error…

-.234567 (If (Left of “.”) contains “-“ then the length of (Left of “.” ) minimum = 2).

1-2.123456 (If (Left of “.”) contains “-“ it should be the first character.

I understand your solution is far better because it reduces the amount of code etc. but I am not ready to rebuild my app from the ground-up yet. I need to learn and understand Swift alot more before I tackle that.

// In this example “coordinate” is either Latitude or Longitude
// Error example = 00.000000

// If coordinate contains more than one  “-“ 
if(coordinate.filter( { $0 == "-" } ).count > 1) {
            coordinate = "00.000000" // Reset coordinate
            }

// If coordinate contains more than one  “.“
if(coordinate.filter( { $0 == "." } ).count != 1) {
                coordinate = "00.000000" // Reset coordinate
            }

// If coordinate contains an illegal character
// Get coordinate length before check
let coordinateLengthBefore = coordinate.count 
let validCharsCoordinate: Set<Character> = ["-", ".", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9"] // Allowed characters
// Remove characters not in Character Set
coordinate.removeAll(where: { !validCharsCoordinate.contains($0) } )
// Get coordinate length after check
let coordinateLengthAfter = coordinate.count
// If coordinate length is different due to illegal character removal
        if coordinateLengthBefore != coordinateLengthAfter {
                coordinate = "00.000000" // Reset coordinate
            }

// Check each side of the "."
// Check coordinate before the "."
        let coordinateStr = coordinate
        let coordinateCh = Character(".")
        let coordinateResult = coordinateStr.split(separator: coordinateCh).map         { String($0) }
        let coordinateLeftSplit = coordinateResult[0]
        let coordinateLeftCount = coordinateLeftSplit.count
        if coordinateLeftCount >= 4 { // More than 3 characters before "."
cards.firstCardLatitude = "00.000000" // Reset coordinate
            }
// Check coordinate after the "."
        let coordinateRightSplit = coordinateResult[1]
if(coordinateRightSplit.filter( { $0 == "-" } ).count > 0) {
                coordinate = "00.000000" // Reset coordinate ("-" not allowed)
            }