← back to the blog

Split string to array SwiftUI / Swift 5.1

Posted on January 1st, 2020 in Swift by George


In one of my applications, I had a requirement to extract some text values out of a string looking like this.


var stringValue = "status:05:taxed:true"


 I knew that the string value will come from the API always in this format. But I only needed the value after the status: part.


Using the .components method on the stringValue will break the string into an array of substrings. Use separtedBy delimiter to define the boundaries for the string to be split at. 

This allows me to access the status code as on the second line in the example below.


let arrayOfValues = stringValue.components(separatedBy: ":")

let statusCode = arrayOfValues[1];


Thank you