Xcode 需要在完成处理程序中的 return 值名称之前放置 _
Xcode requires to put _ before return value name in completion handler
我有一个带有 return 值字符串的完成处理程序函数。
func hardProcessingWithString(input: String, completion: (result: String) -> Void) {}
但是,Xcode 要求我在 return 值名称前加上 _,所以我必须在结果
前加上 _
func hardProcessingWithString(input: String, completion: (_ result: String) -> Void) {}
因此,当我调用我的函数时,我的 return 值没有名称,而是显示此
hardProcessingWithString(input: String, completion: (String) -> Void)
有没有办法不显示
completion: (String) -> Void)
我想让它显示
completion: result -> Void)
这样我就知道 return 值的含义而不是完成类型。谢谢!
自 Swift 的某些版本起,闭包类型不能有参数名称。但是,有一个解决方法:
typealias Result = String
现在您可以使用 Result
作为闭包类型:
func f(completion: (Result) -> Void) { ... }
Foundation
中的许多方法也是这样做的。例如 TimeInterval
只是 Double
的别名,但将其命名为 TimeInterval
使目的更明确。
这是设计使然(尽管可以肯定的是,并不是每个人都喜欢它)。参见
https://bugs.swift.org/browse/SR-2894
正如乔丹·罗斯所解释的那样:
This is correct behavior. The argument labels are considered part of a function's name, not its type. A function value / closure has only a base name and no argument labels, so they cannot be specified.
您不应为闭包定义添加标签。就这么定义吧。
func hardProcessingWithString(input: String, completion: (String) -> Void) {}
调用此函数时,您应该定义标签。
hardProcessingWithString(input: "") { (result) in
}
我有一个带有 return 值字符串的完成处理程序函数。
func hardProcessingWithString(input: String, completion: (result: String) -> Void) {}
但是,Xcode 要求我在 return 值名称前加上 _,所以我必须在结果
前加上 _func hardProcessingWithString(input: String, completion: (_ result: String) -> Void) {}
因此,当我调用我的函数时,我的 return 值没有名称,而是显示此
hardProcessingWithString(input: String, completion: (String) -> Void)
有没有办法不显示
completion: (String) -> Void)
我想让它显示
completion: result -> Void)
这样我就知道 return 值的含义而不是完成类型。谢谢!
自 Swift 的某些版本起,闭包类型不能有参数名称。但是,有一个解决方法:
typealias Result = String
现在您可以使用 Result
作为闭包类型:
func f(completion: (Result) -> Void) { ... }
Foundation
中的许多方法也是这样做的。例如 TimeInterval
只是 Double
的别名,但将其命名为 TimeInterval
使目的更明确。
这是设计使然(尽管可以肯定的是,并不是每个人都喜欢它)。参见
https://bugs.swift.org/browse/SR-2894
正如乔丹·罗斯所解释的那样:
This is correct behavior. The argument labels are considered part of a function's name, not its type. A function value / closure has only a base name and no argument labels, so they cannot be specified.
您不应为闭包定义添加标签。就这么定义吧。
func hardProcessingWithString(input: String, completion: (String) -> Void) {}
调用此函数时,您应该定义标签。
hardProcessingWithString(input: "") { (result) in
}