Nullable 和 Non Nullable 型別
普通型別,如 String
,不可為空。為了使它們能夠儲存空值,你必須通過在它們後面加上 ?
來明確表示:String?
var string : String = "Hello World!"
var nullableString: String? = null
string = nullableString // Compiler error: Can't assign nullable to non-nullable type.
nullableString = string // This will work however!