darkpaw thank you for taking the time to help me with this…
Your code works perfectly but unfortunately not when inserted in to my app.
I have copied and pasted my attempt to get it to work and have added some more of my code to hopefully better explain.
I understand it may not work due to some code & variables missing and is only for explanation purposes.
Thank you again for your help so far and I am hoping you can find the time and patience to help a “newbie” despite being elderly!
import SwiftUI
// Latitude: ^(\+|-)?(?:90(?:(?:\.0{1,6})?)|(?:[0-9]|[1-8][0-9])(?:(?:\.[0-9]{1,6})?))$
// Longitude: ^(\+|-)?(?:180(?:(?:\.0{1,6})?)|(?:[0-9]|[1-9][0-9]|1[0-7][0-9])(?:(?:\.[0-9]{1,6})?))$
var scanArray: [String] = [] // From my app
var gameArray: [String] = [] // From my app
// From my app
private var emptyData = "1,,00.000000,00.000000,,,,,,,,2,,00.000000,00.000000,,,,,,,,3,,00.000000,00.000000,,,,,,,,4,,00.000000,00.000000,,,,,,,,5,,00.000000,00.000000,,,,,,,,6,,00.000000,00.000000,,,,,,"
// See func bottom of Struct that builds the gameArray below
class Cards: ObservableObject { // From my app
@Published var firstCardLatitude = gameArray [2]
@Published var firstCardLongitude = gameArray [3]
// etc. etc.
@Published var latitudeFormatIncorrect = "Latitude (May start with a minus and at least 1 number then a Full-Stop followed by up to 6 numbers)"
@Published var longitudeFormatIncorrect = "Longitude (May start with a minus and at least 1 number then a Full-Stop followed by up to 6 numbers)"
}
let validCoordCharacters: Set<Character> = ["-", ".", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9"]
struct ContentView: View {
@StateObject var cards = Cards() // From my app
//@State private var cards: card = .init(firstCardLatitude: "-12.345678", firstCardLongitude: "3.987654") // Error "Cannot find card in scope" I understand this is because your Struct card { is commented out. If I comment this out the error disappears but with no errors Playgrounds Crashes during the Build.
@State private var latitudeResult: String = "No result"
@State private var longitudeResult: String = "No result"
var body: some View {
VStack { // From my app
Spacer()
.onAppear(perform: {
buildScan()
})
}
VStack {
Text("Latitude")
TextField("Enter latitude", text: $cards.firstCardLatitude)
.multilineTextAlignment(.center)
.onAppear(perform: {
if cards.firstCardLatitude.wholeMatch(of: /^(\+|-)?(?:90(?:(?:\.0{1,6})?)|(?:[0-9]|[1-8][0-9])(?:(?:\.[0-9]{1,6})?))$/) != nil {
latitudeResult = "Match"
// cards.firstCardLatitude = cards.firstCardLatitude
} else {
latitudeResult = "No match"
// You can uncomment this line to reset the value, if you want
// cards.firstCardLatitude = "000.000000"
// cards.firstCardLatitude = latitudeFormatIncorrect
}
cards.firstCardLatitude.removeAll(where: { !validCoordCharacters.contains($0) } )
})
.onChange(of: cards.firstCardLatitude) {
cards.firstCardLatitude.removeAll(where: { !validCoordCharacters.contains($0) } )
}
Text("Result: \(latitudeResult)")
Divider()
Text("Longitude")
TextField("Enter longitude", text: $cards.firstCardLongitude)
.multilineTextAlignment(.center)
.onAppear(perform: {
if cards.firstCardLongitude.wholeMatch(of: /^(\+|-)?(?:180(?:(?:\.0{1,6})?)|(?:[0-9]|[1-9][0-9]|1[0-7][0-9])(?:(?:\.[0-9]{1,6})?))$/) != nil {
longitudeResult = "Match"
} else {
longitudeResult = "No match"
// You can uncomment this line to reset the value, if you want
// cards.firstCardLongitude = "000.000000"
// cards.firstCardLongitude = longitudeFormatIncorrect
}
cards.firstCardLongitude.removeAll(where: { !validCoordCharacters.contains($0) } )
})
.onChange(of: cards.firstCardLongitude) {
cards.firstCardLongitude.removeAll(where: { !validCoordCharacters.contains($0) } )
}
Text("Result: \(longitudeResult)")
}
.padding()
}
}
func buildScan() { // From my app
scanArray = emptyData.components(separatedBy: ",")
gameArray = scanArray
}
/*struct card { // I Want to replace this with what I have ( class Cards: ObservableObject { )
var firstCardLatitude: String
var firstCardLongitude: String
}*/
/*#Preview {
ContentView()
}*/