Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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]]

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading