Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save codelynx/2cc015cba457cab5bd0c08d0d4f7e692 to your computer and use it in GitHub Desktop.
Save codelynx/2cc015cba457cab5bd0c08d0d4f7e692 to your computer and use it in GitHub Desktop.
Convert between CGFloat and String
// Note:
// When you found that you cannot cast/convert between CGFloat and String, you may add following piece of code
// to make your life a bit easier.
//
// if let number = CGFloat("1234.5") {
// }
// let string = String(CGFloat.pi)
//
extension CGFloat: LosslessStringConvertible {
public init?(_ description: String) {
if let number = Double(description) { self = CGFloat(number) }
else { return nil }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment