什么时候使用类型别名?
When to use typealias?
到目前为止,我了解到类型别名是现有类型的命名别名。通过使用 typealias,我可以做类似的事情:
typealias MyString = String
var str: MyString?
typealias Strings = [String]
var strs: Strings?
这导致将 str
变量声明为字符串,将 strs
声明为字符串数组。
即使是自定义类型:
class MyClass {}
typealias MyClsType = MyClass
var myClass: MyClsType
不过,好像有点没用;从逻辑上讲,声明 - 例如 - var str: MyString?
为字符串而不是 var str: String?
的目的是什么?更重要的是,var str: String
更具表现力。
实际上,毫无疑问,为 - 假设 - String: typealias MyString = String
创建类型别名不会那么有用,(我还假设为具有特定 [=46 的 Dictionary 声明类型别名=] type: typealias CustomDict = Dictionary<String, Int>
可能对你没那么有用。
但是,当谈到使用 compound types 时,您肯定会注意到类型别名的好处。
示例:
考虑到您正在实施管理器,该管理器反复使用其函数中包含许多参数的闭包:
class MyManager {
//...
func foo(success: (_ data: Data, _ message: String, _ status: Int, _ isEnabled: Bool) -> (), failure: (_ error: Error, _ message: String, _ workaround: AnyObject) -> ()) {
if isSuccess {
success(..., ..., ..., ...)
} else {
failure(..., ..., ...)
}
}
func bar(success: (_ data: Data, _ message: String, _ status: Int, _ isEnabled: Bool) -> (), failure: (_ error: Error, _ message: String, _ workaround: AnyObject) -> ()) {
if isSuccess {
success(..., ..., ..., ...)
} else {
failure(..., ..., ...)
}
}
// ...
}
如您所见,方法签名看起来真的乏味!这两种方法都采用 success
和 failure
参数,它们中的每一个都是带参数的闭包;另外,为了实现类似的功能,一直复制粘贴参数是不合逻辑的。
对这种情况实施 typealias
非常合适:
class MyManager {
//...
typealias Success = (_ data: Data, _ message: String, _ status: Int, _ isEnabled: Bool) -> ()
typealias Failure = (_ error: Error, _ message: String, _ workaround: AnyObject) -> ()
func foo(success: Success, failure: Failure) {
if isSuccess {
success(..., ..., ..., ...)
} else {
failure(..., ..., ...)
}
}
func bar(success: Success, failure: Failure) {
if isSuccess {
success(..., ..., ..., ...)
} else {
failure(..., ..., ...)
}
}
// ...
}
因此它会更具表现力和可读性。
此外,你可能想看看我发布的medium story。
我使用 typealias 的常用方法是使用闭包:
typealias VoidClosure = () -> Void
func updateFrom(completion: @escaping VoidClosure) { }
到目前为止,我了解到类型别名是现有类型的命名别名。通过使用 typealias,我可以做类似的事情:
typealias MyString = String
var str: MyString?
typealias Strings = [String]
var strs: Strings?
这导致将 str
变量声明为字符串,将 strs
声明为字符串数组。
即使是自定义类型:
class MyClass {}
typealias MyClsType = MyClass
var myClass: MyClsType
不过,好像有点没用;从逻辑上讲,声明 - 例如 - var str: MyString?
为字符串而不是 var str: String?
的目的是什么?更重要的是,var str: String
更具表现力。
实际上,毫无疑问,为 - 假设 - String: typealias MyString = String
创建类型别名不会那么有用,(我还假设为具有特定 [=46 的 Dictionary 声明类型别名=] type: typealias CustomDict = Dictionary<String, Int>
可能对你没那么有用。
但是,当谈到使用 compound types 时,您肯定会注意到类型别名的好处。
示例:
考虑到您正在实施管理器,该管理器反复使用其函数中包含许多参数的闭包:
class MyManager {
//...
func foo(success: (_ data: Data, _ message: String, _ status: Int, _ isEnabled: Bool) -> (), failure: (_ error: Error, _ message: String, _ workaround: AnyObject) -> ()) {
if isSuccess {
success(..., ..., ..., ...)
} else {
failure(..., ..., ...)
}
}
func bar(success: (_ data: Data, _ message: String, _ status: Int, _ isEnabled: Bool) -> (), failure: (_ error: Error, _ message: String, _ workaround: AnyObject) -> ()) {
if isSuccess {
success(..., ..., ..., ...)
} else {
failure(..., ..., ...)
}
}
// ...
}
如您所见,方法签名看起来真的乏味!这两种方法都采用 success
和 failure
参数,它们中的每一个都是带参数的闭包;另外,为了实现类似的功能,一直复制粘贴参数是不合逻辑的。
对这种情况实施 typealias
非常合适:
class MyManager {
//...
typealias Success = (_ data: Data, _ message: String, _ status: Int, _ isEnabled: Bool) -> ()
typealias Failure = (_ error: Error, _ message: String, _ workaround: AnyObject) -> ()
func foo(success: Success, failure: Failure) {
if isSuccess {
success(..., ..., ..., ...)
} else {
failure(..., ..., ...)
}
}
func bar(success: Success, failure: Failure) {
if isSuccess {
success(..., ..., ..., ...)
} else {
failure(..., ..., ...)
}
}
// ...
}
因此它会更具表现力和可读性。
此外,你可能想看看我发布的medium story。
我使用 typealias 的常用方法是使用闭包:
typealias VoidClosure = () -> Void
func updateFrom(completion: @escaping VoidClosure) { }