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)
}
网友评论