Splitting a String into an Array
suggest changecomponents(separatedBy:) splits a string on a separator. It is a Foundation method. Swift 2 called it componentsSeparatedByString.
let startDate = "23:51"
let startDateAsArray = startDate.components(separatedBy: ":") // ["23", "51"]
Swift 2:
let startDate = "23:51"
let startArray = startDate.componentsSeparatedByString(":") // ["23", "51"]
Or when the separator isn’t present:
let myText = "MyText"
let myTextArray = myText.components(separatedBy: " ") // myTextArray is ["MyText"]
Swift 2:
let myText = "MyText"
let myTextArray = myText.componentsSeparatedByString(" ") // myTextArray is ["MyText"]
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents