Post

Replies

Boosts

Views

Activity

Reply to Functions performed elsewhere rather than included in a view
I have worked it out and in doing so have dispelled the myth I was spreading unknowingly! I managed to move all the functions to one View. I am mystified because I am sure Swift wasn’t letting me when I first started building my app (Unfortunately too long ago to know why). The only difference now is this new View has very little viewable content.
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Oct ’23
Reply to Unable to update my apps on Playground
Thank you for your reply. Initially it was indeed an agreement that needed attention. I have done that but in the confusion I have also agreed to a Paid App Agreement which now means I am required to supply a Tax Number etc. The trouble is I only have two apps that are free. Also I am retired and in Australia am not required to have a Tax Number etc. I am now in the process of sorting this out.
Dec ’24
Reply to Checking the contents of a TextField variable method not working
TextField("Latitude (May start with a minus and at least 1 number then a Full-Stop followed by up to 6 numbers)", text: $cards.firstCardLatitude) .multilineTextAlignment(.center) .onAppear(perform: { let charArray = Array(cards.firstCardLatitude) // Convert Text Field to an Array of Characters? (“-“, “3”, “2”, “.”, “1”, “2”, “3”, “4”, “)”, ”6”) (between 4 and 6 “)” not allowed) let allowed: [Character] = ["-", ".", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9"] // Only these Characters are allowed for char in charArray { // Check each Character in Text Field input for unwanted Characters (only allowed are allowed) if (!allowed.contains(char)) { // If an unwanted Character is present then reset Text Field to 000.000000 cards.firstCardFirstWord = "000.000000" // When the Form is checked (next view) another trap looking for 000.000000 prevents the form from being submitted. User has to go back to that Card and try again. } } })
Topic: UI Frameworks SubTopic: SwiftUI Tags:
May ’25
Reply to Checking the contents of a TextField variable method not working
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() }*/
Topic: UI Frameworks SubTopic: SwiftUI Tags:
May ’25
Reply to Checking the contents of a TextField variable method not working
Apologies for experimenting with your Code but I was trying to add more Text (Card Number) and TextFields etc. contained in a Card. I was under the impression that your View that contained the Latitude and Longitude TextFields could be added to with other fields ? I have added a TextField to your Code without further modification and the result is the same. Again thank you and do understand if you don’t want to assist me any further. // ContentView.swift import SwiftUI struct ContentView: View { // This gets the first card where the name property is "Card1" @ObservedObject private var card: Card = cards.first(where: { $0.name == "Card1" }) ?? Card.emptyCard var body: some View { VStack { // In order to reduce duplicated code, I moved the TextField and related stuff into its own View struct CoordEntryView(isLatitude: true, card: card) Divider() CoordEntryView(isLatitude: false, card: card) } .padding() } } struct CoordEntryView: View { var isLatitude: Bool // Allows us to use this same code for the two different coorindates @ObservedObject var card: Card @State private var valueOk: Bool = true var body: some View { // Added this TextField to your Code TextField("Name", text: ($card.name)) .multilineTextAlignment(.center) .font(.system(size: 36)) .frame(maxWidth: 300) .background( RoundedRectangle(cornerRadius: 8) .fill(Color.blue.opacity(0.15)) ) Text(isLatitude ? "Latitude" : "Longitude") .bold() TextField("Enter value", text: (isLatitude ? $card.coord.latitude : $card.coord.longitude)) .multilineTextAlignment(.center) .font(.system(size: 36)) .frame(maxWidth: 300) .background( RoundedRectangle(cornerRadius: 8) .fill(Color.blue.opacity(0.15)) ) .onAppear(perform: { checkCoords() }) .onChange(of: (isLatitude ? card.coord.latitude : card.coord.longitude)) { checkCoords() } if(!valueOk) { Text(Coordinate.formatIncorrect) .foregroundStyle(.red) } } private func checkCoords() { // Checks the coordinate value is valid, removes irrelevant characters, makes sure the minus sign is at the beginning, and trims to the right length if(isLatitude) { valueOk = (card.coord.latitude.wholeMatch(of: /^(\+|-)?(?:90(?:(?:\.0{1,6})?)|(?:[0-9]|[1-8][0-9])(?:(?:\.[0-9]{1,6})?))$/) != nil) card.coord.latitude = Coordinate.validate(card.coord.latitude) // Trim to the right length card.coord.latitude = String(card.coord.latitude.prefix((card.coord.latitude.prefix(1) == "-" ? 10 : 9))) } else { valueOk = (card.coord.longitude.wholeMatch(of: /^(\+|-)?(?:180(?:(?:\.0{1,6})?)|(?:[0-9]|[1-9][0-9]|1[0-7][0-9])(?:(?:\.[0-9]{1,6})?))$/) != nil) card.coord.longitude = Coordinate.validate(card.coord.longitude) // Trim to the right length card.coord.longitude = String(card.coord.longitude.prefix((card.coord.longitude.prefix(1) == "-" ? 11 : 10))) } } } #Preview { ContentView() }
Topic: UI Frameworks SubTopic: SwiftUI Tags:
May ’25
Reply to Checking the contents of a TextField variable method not working
Thanks again, very much appreciated! On first glance you have done what I first tried (creating another Struct etc.) but it didn’t work for me. I will compare yours with mine and hopefully see where I went wrong. I was thinking about posting what I tried prior to my previous post which produced the doubling up for you to see if I am making tracks in the right direction but not sure if it would be appropriate here?
Topic: UI Frameworks SubTopic: SwiftUI Tags:
May ’25
Reply to Checking the contents of a TextField variable method not working
This is the piece I was missing! Thank you! I still think I have much to learn but thanks to you I am making progress! VStack { // In order to reduce duplicated code, I moved the TextField and related stuff into its own View struct CoordEntryView(isLatitude: true, card: card) Divider() CoordEntryView(isLatitude: false, card: card) Divider() TextEntryView_ThreeWords(card: card) } .padding()
Topic: UI Frameworks SubTopic: SwiftUI Tags:
May ’25