Post

Replies

Boosts

Views

Activity

Reply to Clearing an app’s memory, data etc.
Thank you for taking your time to help with this. I have tried your suggestion but I probably need to stress that the app does not crash but just has a problem of a random current View disappearing to show the previous View that contains a Button that opened it. In my app I made sure that after each round a user has to start from the beginning again. This is where I was hoping to do a reset. From my investigations I know you are not allowed to force quit an app programmaticall. I also understand my app is overweight so maybe I need to wait until I redo it starting with what I have learnt from you.
Topic: Community SubTopic: Apple Developers Tags:
May ’25
Reply to Checking the contents of a TextField variable method not working
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) }
Topic: UI Frameworks SubTopic: SwiftUI Tags:
May ’25
Reply to Checking the contents of a TextField variable method not working
Thank you again but yet again out of my depth! Having said that I feel I have learned so much from your help! I am thinking when my head stops spinning I will compare the difference/s between how you have constructed this Form and how I have in my App. I already know your way has far less need for Variables. PS I added ScrollView to make the View scrollable. var body: some View { ScrollView(.vertical, showsIndicators: false) { VStack { ForEach(cards, id: \.self) { card in VStack { Divider() TextEntryView_PartOne(card: card) Divider() CoordEntryView(isLatitude: true, card: card) Divider() CoordEntryView(isLatitude: false, card: card) Divider() TextEntryView_PartTwo(card: card) Divider() } .padding() } } } .padding() }
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
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