Xcode 8 :function types cannot have argument label breaking my build

后端 未结 5 926
有刺的猬
有刺的猬 2020-11-29 04:41

It seems that for some reason Swift have chosen to make coding in it less readable by forcing users to remove completion handler parameter labels. I have read the Swift dis

5条回答
  •  长情又很酷
    2020-11-29 04:58

    A workaround to consider. You can't do:

    func doStuff(completion: (foo: Int, bar: String) -> Void) {
        ...
        completion(foo: 0, bar: "")
    }
    

    ... but you can do:

    func doStuff(completion: ((foo: Int, bar: String)) -> Void) {
        ...
        completion((foo: 0, bar: ""))
    }
    

    i.e. have a single unnamed argument to your closure which is a tuple, in this case (foo: Int, bar: String).

    It's ugly in its own way, but at least you retain the argument labels.

提交回复
热议问题