LoginSignup
1

More than 3 years have passed since last update.

Swift:CGFloatを文字列で初期化する

Posted at
extension CGFloat {
    init?(_ text: String) {
        guard let double = Double(text) else { return nil }
        self = CGFloat(double)
    }
}

let value = CGFloat("1.2345") ?? 0.0

CGFloatはよく使うのにも関わらず文字列から直接生成するやつがなくて不便だったので拡張した.

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1