Closure tuple does not support destructuring in Xcode 9 Swift 4

Viewed 18898

After the gloss project for Swift 4 in Xcode 9

I am getting following error which i have no idea

Closure tuple parameter '(key: _, value: _)' does not support destructuring

Code:

extension Dictionary
{
    init(elements: [Element]) {
        self.init()
        for (key, value) in elements {
            self[key] = value
        }
    }

    func flatMap<KeyPrime, ValuePrime>(_ transform: (Key, Value) throws -> (KeyPrime, ValuePrime)?) rethrows -> [KeyPrime:ValuePrime] {
        return Dictionary<KeyPrime, ValuePrime>(elements: try flatMap({ (key, value) in
            return try transform(key, value)
        }))
    }
}

Error comes at this point try flatMap({ (key, value)in

4 Answers

I just encountered this error as a result of using enumerated().map():

Closure tuple parameter does not support destructuring

I typed the code:

["foo"].enumerated().map

And then pressed Enter 3 times until Xcode autocompleted the closure boilerplate.

The autocomplete seemingly has a bug that causes the above error. The autocomplete produces double-parenthesis ((offset: Int, element: String)) rather than single-parenthesis (offset: Int, element: String).

I fixed it manually and was able to continue:

// Xcode autocomplete suggests:
let fail = ["foo"].enumerated().map { ((offset: Int, element: String)) -> String in
    return "ERROR: Closure tuple parameter does not support destructuring"
}

// Works if you manually replace the "(( _ ))" with "( _ )"
let pass = ["foo"].enumerated().map { (offset: Int, element: String) -> String in
    return "works"
}

Possibly the result of using Xcode 10.0 beta (10L176w)

I'm using Xcode 11.1 and Swift 5, and ran into this error while using enumerated().map(). I think this example simplifies things a little, but in general this is what fixed it for me. The true error was the compiler unable to infer to return value:

// Correct Syntax
let resultModels: [ResultModel] = array.enumerated().map { index, model in
  // code
}

// Results in the error Closure tuple does not support destructuring
let resultModels = array.enumerated().map { index, model in
  // code
}
Related