Post

Replies

Boosts

Views

Activity

Reply to Index out of range error in my function using str.split(separator: ch).map { String($0) }
So sorry to say but I am still getting the error. In this example what is return supposed to do? I understand it is hard to diagnose something when you only get a snippet and for that I am sorry. In my app the code doesn’t return to the Form to make any corrections until all the functions have run and the app crashes if for instance hello.world. or hello..today is entered instead of hello.world.today ie. two elements instead of three. Maybe I need to look at trapping errors at the input stage. Having said that it is not something I know how to do at this stage (more to learn). Thank you again. Regards Guy
Topic: Programming Languages SubTopic: Swift Tags:
Jan ’23
Reply to Index out of range error in my function using str.split(separator: ch).map { String($0) }
To better explain what I am trying to achieve… I am wanting to trap this error eg. (hello.world.) before the cards.firstCardLettersResult = str.split(separator: ch).map { String($0) } line of code so not to crash the app. My other traps do this for the other types of input error by making a message that is the correct format (contain three elements) for cards.firstCardLettersResult = str.split(separator: ch).map { String($0) } to handle. Without a crash my code then shows another View with all the inputs and if any of the inputs are incorrect (typos) or contain any of the trap messages my code has a button in this View to return to the form so inputs can be edited. This method is not perfect because it overwrites the original input in the form with the error message so the error message has to be deleted and word has to be typed again.
Topic: Programming Languages SubTopic: Swift Tags:
Jan ’23
Reply to Index out of range error in my function using str.split(separator: ch).map { String($0) }
I have just had a brainwave, I just need to work out how to code it! The logic is as follows… if the character before the first full-stop is nil then create an error message eg FIRST.WORD.MISSING If the character before the second full-stop (optional: and after the first full-stop) is nil then create an error message eg SECOND.WORD.MISSING If the character after the second full-stop is nil create an error message eg THIRD.WORD.MISSING I may need help with this!
Topic: Programming Languages SubTopic: Swift Tags:
Jan ’23
Reply to Index out of range error in my function using str.split(separator: ch).map { String($0) }
Thank you so much! At the moment I will use let test = firstCardWords.split(separator: ".").map { String($0) } in my App because I understand it. Until I understand how the extension works I won’t use it. I want to understand every aspect of my App so that despite receiving some help in building it and without that help (from you, Claude31) I can feel comfortable I built it. Thanks again Guy
Topic: Programming Languages SubTopic: Swift Tags:
Jan ’23
Reply to Index out of range error in my function using str.split(separator: ch).map { String($0) }
I think I have gone down the wrong path because although this works I haven’t been able to expand on it with suffix etc. (after the first full-stop (second word)) and it doesn’t address after the second full-stop (third word). Back to the drawing board! let threewords = cards.firstCardWords var firstWord = ""         if let index = threewords.firstIndex(of: ".") {             firstWord = String(threewords.prefix(upTo: index))         }         if firstWord == "" {             cards.firstCardWords = "FIRST.WORD.MISSING"         }
Topic: Programming Languages SubTopic: Swift Tags:
Jan ’23
Reply to Displaying two sets of GPS Coordinates on a map
Found this is working for me except for displaying the user’s location. Can someone tell me what I am doing wrong please 🙏 Positions View import MapKit struct Positions: Identifiable {     let id = UUID()     let name: String     let latitude: Double     let longitude: Double     var coordinate: CLLocationCoordinate2D {         CLLocationCoordinate2D(latitude: latitude, longitude: longitude)     } } Map View import MapKit let thisCardPositionLatitude = -27.907625 let thisCardPositionLongitude = 152.680835 let nextCardPositionLatitude = -27.908190 let nextCardPositionLongitude = 152.679676     struct MapView: View {         @State private var region = MKCoordinateRegion(             center: CLLocationCoordinate2D(                 latitude: thisCardPositionLatitude,                 longitude: thisCardPositionLongitude),             span: MKCoordinateSpan(                 latitudeDelta: 0.00001,                 longitudeDelta: 0.00001)         )         private let places = [             Positions(name: "This Card Position", latitude: thisCardPositionLatitude, longitude: thisCardPositionLongitude),             Positions(name: "Next Card Position", latitude: nextCardPositionLatitude, longitude: nextCardPositionLongitude)         ]         var body: some View {             Map(coordinateRegion: $region,                 showsUserLocation: true,                 userTrackingMode: .constant(.follow),                 annotationItems: places){ place in                 MapMarker(coordinate: place.coordinate,tint: Color.accentColor)             }                 .edgesIgnoringSafeArea(.all)         }     }     struct MapView_Previews: PreviewProvider {         static var previews: some View {             MapView()         }     }
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Feb ’23
Reply to Is this bad practice?
am a “newbie” to Coding and Swift. To better understand the Code I have started breaking it up into chunks and formatting as per my example below. My questions are 1) is “Is this bad practice”? 2) “Is it likely to lead to problems later on”?
Topic: Programming Languages SubTopic: Swift Tags:
Feb ’23
Reply to Guidance Needed: TextField, Import UI & Other Errors
Hello, firstly I also am relatively new to SwiftUI and am working on my first app. Secondly I don’t actually know the answer to your question and certainly don’t want to confuse you but wanted to show you another way to approach Form Filling. This way works well but if it doesn’t suit your needs I certainly won’t be offended. The code I have supplied is by no way complete but I have tried to give you the necessary chunks for you to hopefully get an idea. I will also attach a screenshot to show the resulting Form. I hope this helps in some way. class Cards: ObservableObject { @Published var firstCardNumber = "1" @Published var firstCardWords = gameArray [1] @Published var firstCardLatitude = gameArray [2] @Published var firstCardLongitude = gameArray [3] @Published var firstCardFirstWordClue = gameArray [4] @Published var firstCardSecondWordClue = gameArray [5] @Published var firstCardThirdWordClue = gameArray [6] @Published var firstCardAnswerClue = "" @Published var firstCardFirstLetters = "" @Published var firstCardSecondLetters = "" @Published var firstCardThirdLetters = "" @Published var firstCardFirstWord = "" @Published var firstCardSecondWord = "" @Published var firstCardThirdWord = "" @Published var firstCardLettersResult = [] @Published var firstCardWordClueResult = [] @Published var secondCardNumber = "2" etc. @State private var submit = false @State private var count = 0 @StateObject var cards = Cards() @State var firstCards = (String)() @State var firstCardsJoin = (String)() @State var secondCards = (String)() @State var secondCardsJoin = (String)() etc. VStack (alignment: .center){ Text("Game Form") .font(.headline) .bold() .frame(maxWidth: .infinity, alignment: .center) .onAppear(perform: { count = 1 firstCardsJoined () }) } Section { TextField("Card Number:", text: $cards.firstCardNumber) .multilineTextAlignment(.center) SecureField("full-stops.between.words", text: $cards.firstCardWords) .textCase(.lowercase) .keyboardType(.alphabet) .disableAutocorrection(true) .textInputAutocapitalization(.never) .multilineTextAlignment(.center) TextField("Latitude (North-South)", text: $cards.firstCardLatitude) .multilineTextAlignment(.center) TextField("Longitude (East-West)", text: $cards.firstCardLongitude) .multilineTextAlignment(.center) TextField("First Word Clue. Maximum Charaters : 80", text: $cards.firstCardFirstWordClue) .multilineTextAlignment(.center) TextField("Second Word Clue. Maximum Charaters : 80", text: $cards.firstCardSecondWordClue) .multilineTextAlignment(.center) TextField("Third Word Clue. Maximum Charaters : 80", text: $cards.firstCardThirdWordClue) .multilineTextAlignment(.center) } Section { TextField("Card Number:", text: $cards.secondCardNumber) .multilineTextAlignment(.center) SecureField("full-stops.between.words", text: $cards.secondCardWords) .textCase(.lowercase) .keyboardType(.alphabet) .disableAutocorrection(true) .textInputAutocapitalization(.never) .multilineTextAlignment(.center) TextField("Latitude (North-South)", text: $cards.secondCardLatitude) .multilineTextAlignment(.center) TextField("Longitude (East-West)", text: $cards.secondCardLongitude) .multilineTextAlignment(.center) TextField("First Word Clue. Maximum Charaters : 80", text: $cards.secondCardFirstWordClue) .multilineTextAlignment(.center) TextField("Second Word Clue Maximum Charaters : 80", text: $cards.secondCardSecondWordClue) .multilineTextAlignment(.center) TextField("Third Word Clue Maximum Charaters : 80", text: $cards.secondCardThirdWordClue) .multilineTextAlignment(.center) } etc. self.submit.toggle() if count == 1 { firstCardsJoined() } }) { Text("✅ Okay ✅") .font(.headline) .foregroundColor(.red) .frame(maxWidth: .infinity, alignment: .center) .padding() }.sheet(isPresented: $submit) { FormResultView(cards: cards) } } } func firstCardsJoined () { firstCardsJoin = cards.firstCardNumber + "," + cards.firstCardWords + "," + cards.firstCardLatitude + "," + cards.firstCardLongitude + "," + cards.firstCardFirstWordClue + "," + cards.firstCardSecondWordClue + "," + cards.firstCardThirdWordClue + ",,,," firstCardsLettersJoined() } func firstCardsLettersJoined () { firstCards = firstCardsJoin secondCardsJoined() } func secondCardsJoined () { secondCardsJoin = cards.secondCardNumber + "," + cards.secondCardWords + "," + cards.secondCardLatitude + "," + cards.secondCardLongitude + "," + cards.secondCardFirstWordClue + "," + cards.secondCardSecondWordClue + "," + cards.secondCardThirdWordClue + ",,,," secondCardsLettersJoined() } func secondCardsLettersJoined () { secondCards = secondCardsJoin thirdCardsJoined() } etc. cards.sharedFile = firstCards + "," + secondCards + "," + thirdCards + "," + fourthCards + "," + fifthCards + "," + sixthCards } Array Builder @ObservedObject var cards = Cards() @State var firstCards = (String)() @State var firstCardsJoin = (String)() @State var firstCardLetters = (String)() @State var secondCards = (String)() etc. func sixthCardsLettersJoined () { sixthCardLetters = cards.sixthCardFirstLetters + "," + cards.sixthCardSecondLetters + "," + etc. func allCardsJoined () { formData = firstCards + "," + secondCards + "," + thirdCards + "," + fourthCards + "," + fifthCards + "," + sixthCards formArray = formData.components(separatedBy: ",") gameArray = formArray }
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