I have a String
, e.g. "7,8,9,10"
that I'd like to convert to an array of Int
items i.e. [Int]
However, my code currently gives me a [Int?]
instead (an array of optional integers). Here is the code:
let years = (item["years"] as! String)
.componentsSeparatedByString(",")
.map { word in Int(word) }
I tried a few things, like changing Int(word)
to Int(word!)
, and adding !
to the end of String)
, but swift didn't like the look of those ideas.
I presume there's something obvious I'm doing wrong as a beginner, but I'm not quite sure what! Any help would be greatly appreciated - thank you!