LoginSignup
18

More than 5 years have passed since last update.

Swiftのif letでは二つ以上の変数を同時に束縛できないのでその代替方法

Last updated at Posted at 2014-07-08

SwiftでOptionalタイプの変数からif letで値を取り出して利用しますが、現状では二つ以上の変数を同時にif letで扱うことができません。

二つ以上のOptionalタイプの変数から値を取り出す場合には以下のようにswitchを利用します。

let first : Int? = "1".toInt()
let second : Int? = "2".toInt()

switch (first, second) {
case (.Some(let f), .Some(let s)):
    println(f + s)
default:
    println("error")
}

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
18