The three lines of code (//) that attempt to pass the contents from one variable to another crashes the app.
I added the (as! String) as suggested by the compiler because this error appeared instantly “ Cannot assign value of type 'Any' to type 'String' Insert " as! String" ”
I am still very new to Coding so this has me really stumped. Can someone shine some light on this please!
func extractFirstCardWordLetters () {
let str = "happy.new.year.to.one.and.all"
let ch = Character(".")
cards.firstCardLettersResult = str.split(separator: ch)
//cards.firstCardFirstLetters = cards.firstCardLettersResult [0] as! String
//cards.firstCardSecondLetters = cards.firstCardLettersResult [1] as! String
//cards.firstCardThirdLetters = cards.firstCardLettersResult [2] as! String
print(cards.firstCardLettersResult) // prints ["happy", "new", "year", "to", "one", "and", "all"]
print(cards.firstCardLettersResult[0]) // prints happy
print(cards.firstCardLettersResult[1]) // prints new
print(cards.firstCardLettersResult[2]) // prints year
}
You don't show enough code to fully understand what happens.
What is the crash message ? Probably trying to unwrap nil ?
Show the complete code, please.
I created a Cards struct to check
struct Cards {
var firstCardLettersResult = [String]()
var firstCardFirstLetters = ""
var firstCardSecondLetters = ""
var firstCardThirdLetters = ""
}
var cards = Cards()
func extractFirstCardWordLetters() {
let str = "happy.new.year.to.one.and.all"
let ch = Character(".")
cards.firstCardLettersResult = str.split(separator: ch).map { String($0) }
cards.firstCardFirstLetters = cards.firstCardLettersResult [0]
cards.firstCardSecondLetters = cards.firstCardLettersResult [1]
cards.firstCardThirdLetters = cards.firstCardLettersResult [2]
print(cards.firstCardLettersResult) // prints ["happy", "new", "year", "to", "one", "and", "all"]
print(cards.firstCardLettersResult[0]) // prints happy
print(cards.firstCardLettersResult[1]) // prints new
print(cards.firstCardLettersResult[2]) // prints year
}
extractFirstCardWordLetters()
I get the following log:
["happy", "new", "year", "to", "one", "and", "all"]
happy
new
year
Problem comes from here:
cards.firstCardLettersResult = str.split(separator: ch)
You have probably defined cards.firstCardLettersResult as [String]
But split returns [subString]. So you need to convert each to String. That's what the map does:
cards.firstCardLettersResult = str.split(separator: ch).map { String($0) }
If you are not familiar with map, it takes each item in the array, applies the change and puts in the new array. In a more easy to read form:
cards.firstCardLettersResult = str.split(separator: ch).map { subString in return String(subString) }
Note: print(cards.firstCardLettersResult) works, because it does make the conversion internally.