Edward Anthony Edward Anthony -3 years ago 77
Swift Question

Parse String containing "Array of String" into Array using Swift 3.0

I'm looking for a way to convert this string into array using Swift 3.0

let arrayString: String = "[\"One\", \"Two\", \"Three\", \"Four\"]"

// Should be converted into:

let array: [String] = [

If I separate it into token using
as the separator, it will include
so it won't work. How to convert this properly?


The question is different from:
Simple and clean way to convert JSON string to Object in Swift

I'm not trying to convert an JSON Object that has multiple keys in it.

Answer Source

it's a JSON String, you have to convert it in normal string array by using JSONSerialization.

Swift 3.0

func convertToArray(str: String) -> [String]? {

    let data = str.data(using: .utf8)

    do {
        return try JSONSerialization.jsonObject(with: data!, options: []) as? [String]
    } catch {

   return nil


let aString: String = "[\"One\", \"Two\", \"Three\", \"Four\"]"

let arrayString = convertToArray(str: aString)

print(arrayString) // ["One", "Two", "Three", "Four"]
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download