Swift - 可选型的实际应用

Optional 在元组中的应用
var error1 : (errorCode: Int, errorMessage: String?) = ( 404, "Not fount")

error1.errorMessage = nil

var error2 : (errorCode: Int, errorMessage: String)? = ( 404, "Not fount")
//error2?.errorMessage = nil  ❎
error2 = nil

var error3 : (errorCode: Int, errorMessage: String?)? = ( 404, "Not fount")
error3?.errorMessage = nil    ✔️
error3 = nil
系统库中的实际应用,如:
var ageInput : String = "16"

var age = Int( ageInput )

if let age = Int(ageInput), age < 20{
    print(age)
}

你可能感兴趣的:(Swift - 可选型的实际应用)