let str: String? = "Hello, playground"; print(str!); //肯定該變量有值,則直接進行解包便可 // Optional Binding if let tempStr = str { print(tempStr) }
let possibleString: String? = "An optional string." print(possibleString!) let assumedString: String! = "An implicitly unwrapped optional string." print(assumedString)
if (assumedString != nil) { print(assumedString) }
if let definiteString = assumedString { print(definiteString) }
var username: String? var password: String? switch (username, password){ case let (username?, password?): // 這裏的 ? 是Swift2.0以後新增的語法,表示有值 print("都有值") break case let (usermane?, nil): print("請輸入密碼") break case let (nil, password?): print("請輸入帳號") break case let (nil, nil): print("請輸入帳號密碼") break default: break }