多重 Optional

/*

Optional

我们使用类型后面加上?的语法只不过是Optional类型的语法糖,而实际这个类型是一个enum:

*/

publicenumOptional :ExpressibleByNilLiteral{

/// The absence of a value.

///

/// In code, the absence of a value is typically written using the `nil`

/// literal rather than the explicit `.none` enumeration case.

casenone

/// The presence of a value, stored as `Wrapped`.

casesome(Wrapped)

// ...

}

/*

在Optional中没有对Wrapped进行任何限制,可以是任何类型(String, Int,另一个Optional)

*/

varstring:String? ="string"//类型String?

varanotherString:String?? =string//类型String??

varaNil:String? =nil

varanotherNil:String?? =aNil//一个大盒子中装有一个小盒子,小盒子里面为空

varliteralNil:String?? =nil//盒子里面为空

ifleta =anotherNil{

print("anotherNil")//这里输出

}

ifletb =literalNil{

print("literalNil")//这里不会输出

}

ifanotherNil==nil{

print("--- anotherNil")//这里不会输出

}

ifliteralNil==nil{

print("--- literalNil")//这里会输出

}

/*

但是通过lldb打印po的时候,都会显示为nil

可以说使用fr v -R命令打印出未加工时的信息

*/


最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容