salocinx salocinx - 5 months ago 49
iOS Question

Swift: How to convert a list of String to a list of CGPoint?

I am a complete newbie to Swift and didn't found anything on the web. How do I convert a string formatted this way:

let str:String = "0,0 624,0 624,-48 672,-48 672,192"


to an array of CGPoint's ?

Answer

This solution uses the CGPointFromString function provided by iOS.

import UIKit

let res = str.componentsSeparatedByString(" ").map { CGPointFromString("{"+$0+"}") }