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!