LoginSignup
0
0

More than 5 years have passed since last update.

swift 3.0 オプショナル型の宣言とチェック方法

Posted at

オプショナル忘備録

AboutOptional.swift

//オプショナル型の2種類の宣言

let s : Optional<String> = "1"
let v : String? = nil

//オプショナルはチェックしないと使えない
//オプショナルの判定は三種類

//チェック方法1
//if文チェック

if s != nil {
    print(s!) //unwrap → sを取り出す処理
}
//ちなみに関数の中にコメントを書いてもエラーは出ません。

//チェック方法2
//Optional Biding
//ex. s != nilの時 unwrapして valueに代入する
if let value = s {
    print(value)
}

//チェック方法3
print(s ?? "this is nil")


0
0
0

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
0
0