繁体   English   中英

在Swift 3中是否可以通过闭包签名重载静态方法?

[英]Is it possible to overload static method by closure signature in Swift 3?

Swift 3中的闭包类型是否可以重载静态方法? 例如,我有一个带有2种方法的结构:

struct Some {
  static func doSomething(first: String, @escaping completion: ([Int]?) -> Void) {
    ...
  }

  static func doSomething(first: String, @escaping completion: ([Int]?, String?) -> Void) {
    ...
  }
}

但是,当我尝试调用第一个方法Some.doSomething(first: "Hello") { (numbers) in ... } (带有一个参数的闭包)编译器给我一个错误:

'doSomething(first:completion :)'的歧义用法

是的,您可以在Swift 3中按闭包类型重载静态方法,但是您需要为第一个函数指定参数的类型,因为其参数与第二个函数的参数部分匹配

 Some.doSomething(first: "") { (number:[Int]?) in

}

Some.doSomething(first: "") { (number, value) in

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM