본문 바로가기
Programming/Swift

[Swift] Optional

by 생각하는달팽이 2016. 2. 10.

Swift 에서는 Safe Power Modern 을 강조했다.

이중에서 Optional 은 Safe 에 해당하는 문법이다.



Optional 은 아래와 같다.





// OPTIONALS

/*

OPTIONAL is for Safety code !

Optional nil 수도 있는 변수 의미하고 선언할  ? 사용한다

일반적인 선언의 경우 Non-Optional 이며 nil 없다.

*/

var score : Int?


//score = 21


// forced unwrapping

//print("The score is currently \(score!)")


if score != nil {

    print("The score is currently \(score)")

}

else{

    print("Could not get score.")

}


// optional binding


if let confirmedScore = score {

    print("The score is currently \(confirmedScore)")

} else{

    print("Could not get the current score.")

}



반응형

'Programming > Swift' 카테고리의 다른 글

[Swift] Classes  (0) 2016.02.10
[Swift] Enumerations  (0) 2016.02.10
[Swift] Tuple  (0) 2016.02.10
[Swift] Set  (0) 2016.02.10
[Swift] Dictionary  (0) 2016.02.10