Swift version: 5.10
You can convert a string to an array by breaking it up by a substring using the components(separatedBy:) method. For example, you can split a string up by a comma and space like this:
let str = "Andrew, Ben, John, Paul, Peter, Laura"
let array = str.components(separatedBy: ", ")
That will return an array of six items, one for each name.
SAVE 50% All our books and bundles are half price for Black Friday, so you can take your Swift knowledge further for less! Get my all-new book Everything but the Code to make more money with apps, get the Swift Power Pack to build your iOS career faster, get the Swift Platform Pack to builds apps for macOS, watchOS, and beyond, or get the Swift Plus Pack to learn Swift Testing, design patterns, and more.
Available from iOS 7.0 – see Hacking with Swift tutorial 5
This is part of the Swift Knowledge Base, a free, searchable collection of solutions for common iOS questions.