Hello everyone!
A few months ago, I was coding a translation app that uses inputs from a textbox and translates that text into a different language. I have been using this code for quite some time now:
var code = [
"A" : "Example",
]
let message = Text.text
var encodedMessage = " "
let array = message!.characters.split(separator: " ")
for singleWord in array {
let word = String(singleWord)
if let encodedWord = code[word] {
encodedMessage += encodedWord
}
else {
encodedMessage += word
}
encodedMessage += " "
}
print(encodedMessage)
All of this worked fine in Swift 4. When I returned to this a few weeks ago and updated to Swift 5, I kept getting an error at this point in the code:
let array = message!.characters.split(separator: " ")
I know that the characters function has been removed and Swift has updated its functions in regards to strings, but I don't know enough about Swift 5 to be able to fix it. Is there a way to work around this issue without having to rewrite the entire code? (Also, apologies for the formatting, I am a bit new to this).
cukr
2
It worked, but was full of warnings saying what you should do instead
'characters' is deprecated: Please use String or Substring directly
In swift4 and swift5 String is a Collection of Characters, so there's no need to use the special view to access them. You should be able to delete the .characters everywhere.
let array = message!.split(separator: " ")
2 Likes
I feel so dumb now. It works perfectly now. Thank you for your help!
1 Like