salocinx salocinx - 1 year ago 108
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 Source

This solution uses the CGPointFromString function provided by iOS.

import UIKit

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