Post

Replies

Boosts

Views

Activity

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
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
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
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 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 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 Guidance Needed: TextField, Import UI & Other Errors
Hello again I hope this explanation helps you better (My explanations of the Code may not be using the correct terminology but they are how I understand them (Layman’s Terms)). @Published var firstCardNumber = "1" // Form Inputs need to be made Observable (Cards:) | struct FormBuilder: View { @StateObject var cards = Cards() // Attaching variables in Form to an ObservableObject (Cards:) | TextField("Card Number:", text: $cards.firstCardNumber) // Creating field in the Form and attaching to Cards: | Text("✅ Okay ✅") .font(.headline) .foregroundColor(.red) .frame(maxWidth: .infinity, alignment: .center) .padding() }.sheet(isPresented: $submit) { FormResultView(cards: cards) // Passing Form Data to another View (Form Result View) } | struct FormResultView: View { @ObservedObject var cards = Cards() // Making the Form Inputs able to be seen in this View var body: some View { | Section { VStack { Text("Card Number: \(cards.firstCardNumber)") // The Form Field Input appears here
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Oct ’23
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:
Replies
Boosts
Views
Activity
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:
Replies
Boosts
Views
Activity
May ’25
Reply to Checking the contents of a TextField variable method not working
I am not familiar with this method of Form making and am not sure how to prevent this doubling up. Can you help please? Also I don’t know how to call the second card to follow the first and the other subsequent cards with this method?
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Replies
Boosts
Views
Activity
May ’25
Reply to Checking the contents of a TextField variable method not working
darkpaw thank you very much for taking the time with this. I will treat it as a learning opportunity to start with but won’t attempt to use it in my app until I understand it and feel confident to do so. Thanks again, much appreciated! P.S. I have created an app in Playgrounds using your code and it works okay.
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Replies
Boosts
Views
Activity
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:
Replies
Boosts
Views
Activity
May ’25
Reply to Checking the contents of a TextField variable method not working
OMG! Thank you so very much! This is exactly what I love (more than solve my problem but actually the opportunity to learn). I will study this to fully understand it which will take some time I think. Having said that this means I probably shouldn’t mark it as solved unless you were able to test it? (Please advise).
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Replies
Boosts
Views
Activity
May ’25
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:
Replies
Boosts
Views
Activity
May ’25
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.
Replies
Boosts
Views
Activity
Dec ’24
Reply to Waiting for TestFlight review for about 3 weeks!
You may have done what I did once. There is a Confirm your submission stage which you may have missed?
Replies
Boosts
Views
Activity
Oct ’23
Reply to Prompt User to Choose Location Preference Not Working
Found this solved my problem and is very informative to boot! [https://youtu.be/poSmKJ_spts?si=z3r3gkDuQtkXtVmb)
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Replies
Boosts
Views
Activity
Oct ’23
Reply to Prompt User to Choose Location Preference Not Working
Have done the neccessary Permission. Also tested it outside of Playgrounds with TestFlight but no success.
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Replies
Boosts
Views
Activity
Oct ’23
Reply to Prompt User to Choose Location Preference Not Working
Further investigated the above and now understand maybe the Code is incomplete? Regarding the second version below which I prefer, I don’t yet see why it doesn’t work.
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Replies
Boosts
Views
Activity
Oct ’23
Reply to Prompt User to Choose Location Preference Not Working
Also tried this without success. class RequestLocationManager: NSObject, CLLocationManagerDelegate { private let locationManager = CLLocationManager() override init() { super.init() locationManager.delegate = self } func requestUserAuthorization() async throws { locationManager.requestWhenInUseAuthorization() } }
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Replies
Boosts
Views
Activity
Oct ’23
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:
Replies
Boosts
Views
Activity
Oct ’23
Reply to Guidance Needed: TextField, Import UI & Other Errors
Hello again I hope this explanation helps you better (My explanations of the Code may not be using the correct terminology but they are how I understand them (Layman’s Terms)). @Published var firstCardNumber = "1" // Form Inputs need to be made Observable (Cards:) | struct FormBuilder: View { @StateObject var cards = Cards() // Attaching variables in Form to an ObservableObject (Cards:) | TextField("Card Number:", text: $cards.firstCardNumber) // Creating field in the Form and attaching to Cards: | Text("✅ Okay ✅") .font(.headline) .foregroundColor(.red) .frame(maxWidth: .infinity, alignment: .center) .padding() }.sheet(isPresented: $submit) { FormResultView(cards: cards) // Passing Form Data to another View (Form Result View) } | struct FormResultView: View { @ObservedObject var cards = Cards() // Making the Form Inputs able to be seen in this View var body: some View { | Section { VStack { Text("Card Number: \(cards.firstCardNumber)") // The Form Field Input appears here
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Replies
Boosts
Views
Activity
Oct ’23