将字符串拆分为数组

在 Swift 中,你可以通过将 String 切片为某个字符,轻松地将 String 分隔为数组:

Version = 3.0

let startDate = "23:51"

let startDateAsArray = startDate.components(separatedBy: ":") // ["23", "51"]`

Version = 2.2

let startDate = "23:51"

let startArray = startDate.componentsSeparatedByString(":") // ["23", "51"]`

或者当分隔符不存在时:

Version = 3.0

let myText = "MyText"

let myTextArray = myText.components(separatedBy: " ") // myTextArray is ["MyText"]

Version = 2.2

let myText = "MyText"

let myTextArray = myText.componentsSeparatedByString(" ") // myTextArray is ["MyText"]