Search code examples
iosswiftcloudkit

Convert Array of [[Double]] to String (similar to Print() output)


let intervals = [[1,1],[2,2],[3,3]]

I would like to store this intervals variables into CloudKit as String in the same form as a print output eg:

print(interval)

output = [[1,1],[2,2],[3,3]]

How can this be achieved? All the solutions from SO and googling only led me to doing a join whereby the arrays would be lost eg: [1,1,2,2,3,3]


Solution

  • You can try

    let intervals = [[1,1],[2,2],[3,3]] 
    let res = "[\(intervals.map { "[\($0.first!),\($0.last!)]" }.joined(separator: ","))]" 
    print(res)
    

    OR

    let res =  String(data: try! JSONEncoder().encode(intervals), encoding: .utf8)!
    

    OR

    let res = String(describing: intervals)