首页 > 解决方案 > Swift:关于使用类型别名屏蔽选项的约定?

问题描述

关于使用掩码可选的类型别名是否有任何约定?例如,如果我想为详细的可选类型(例如 )创建类型别名UnsafeMutablePointer<Bar>?,我可以执行以下任一操作:

// Approach 1 - mask the optional in the type alias
typealias Foo = UnsafeMutablePointer<Bar>?

func bat(baz: Foo) { ... }

// Approach 2 - include the optional in the typealias usage where needed
typealias Foo = UnsafeMutablePointer<Bar>

func bat(baz: Foo?) { ... }

这些方法完成了同样的事情,尽管我认为第二种方法更可取,因为函数参数是可选的似乎更明确。

是否存在第一种方法可能更可取的情况,是否有任何指导方针支持/反对这种用法?

标签: swiftoptionaltype-alias

解决方案


推荐阅读