Post

Replies

Boosts

Views

Activity

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 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 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) }
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) }
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 .replacingOccurrences(of: "\"", with: "") not working
print("cards.firstCardFirstWordClue", cards.firstCardFirstWordClue) // cards.firstCardFirstWordClue A “form” of greeting “ “”” let removeQuotesFirstCardFirstWordClue = cards.firstCardFirstWordClue.replacingOccurrences(of: "\"", with: "") cards.firstCardFirstWordClue = removeQuotesFirstCardFirstWordClue print("removeQuotesFirstCardFirstWordClue", cards.firstCardFirstWordClue) // removeQuotesFirstCardFirstWordClue A “form” of greeting “ “”” let removeCommasFirstCardSecondWordClue = cards.firstCardSecondWordClue.replacingOccurrences(of: ",", with: "") cards.firstCardSecondWordClue = removeCommasFirstCardSecondWordClue print("removeCommasFirstCardSecondWordClue", cards.firstCardSecondWordClue)
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Jan ’23
Reply to .replacingOccurrences(of: "\"", with: "") not working
let removeQuotesFirstCardFirstWordClue = cards.firstCardFirstWordClue.replacingOccurrences(of: "\"", with: "")         cards.firstCardFirstWordClue = removeQuotesFirstCardFirstWordClue         print(cards.firstCardFirstWordClue)         let removeCommasFirstCardSecondWordClue = cards.firstCardSecondWordClue.replacingOccurrences(of: ",", with: "")         cards.firstCardSecondWordClue = removeCommasFirstCardSecondWordClue         print(cards.firstCardSecondWordClue) TextField Input (cards.firstCardFirstWordClue) was… A “form” of greeting Print (cards.firstCardFirstWordClue) result was… A “form” of greeting (quotes still enclosing the word form) TextField Input (cards.firstCardSecondWordClue) was… Another word for, this planet Print (cards.firstCardSecondWordClue) result was… Another word for this planet (comma removed after the word for)
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Jan ’23
Reply to .replacingOccurrences(of: "\"", with: "") not working
let removeQuotesFirstCardFirstWordClue = cards.firstCardFirstWordClue.replacingOccurrences(of: "\"", with: "")         cards.firstCardFirstWordClue = removeQuotesFirstCardFirstWordClue let removeCommasFirstCardSecondWordClue = cards.firstCardSecondWordClue.replacingOccurrences(of: ",", with: "")         cards.firstCardSecondWordClue = removeCommasFirstCardSecondWordClue “\”” doesn’t work on the TextField input cards.firstCardFirstWordClue but "," does. Maybe I need to prevent commas and quotes from being typed into the TextField if that is possible?
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Jan ’23
Reply to Assigning the contents of one Variable to another after split function producing an error
let str = cards.firstCardFirstWordClue         let ch = Character(".")         cards.firstCardLettersResult = str.split(separator: ch).map { String($0) }         cards.firstCardFirstLetters = cards.firstCardLettersResult[0] as! String         cards.firstCardSecondLetters = cards.firstCardLettersResult[1] as! String         cards.firstCardThirdLetters = cards.firstCardLettersResult[2] as! String         makeSecondCardWordLetters() The above works okay with errors. As you suggested I inserted .map. I still needed the as! String after each split. Not sure if .map { subString in return String(subString) } is an alternative form of as! String but it works without it. Thanks again for your help!
Topic: Programming Languages SubTopic: Swift Tags:
Jan ’23
Reply to .replacingOccurrences(of: "\"", with: "") not working
Firstly, thanks once again for taking your time to help me. Unfortunately I won’t be able to show the code for a while (on the road with an iPhone 6). The difference I see between your example and what I am trying to do is, I am trying to remove any accidental stray “ and/or a deliberate “any text“ that maybe put in a Form TextField. I know I tested the stray “ and “””” and they didn’t work but I am pretty sure I didn’t try a normal input like your example. When I am able I will check all three examples and report back. Thanks again and a Happy New Year to you and yours.
Topic: UI Frameworks SubTopic: SwiftUI Tags:
Dec ’22