关闭元组不支持Xcode 9 Swift 4中的解构 [英] Closure tuple does not support destructuring in Xcode 9 Swift 4

查看:327
本文介绍了关闭元组不支持Xcode 9 Swift 4中的解构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Xcode 9中Swift 4的光泽项目之后

After the gloss project for Swift 4 in Xcode 9

我收到以下错误,我不知道

I am getting following error which i have no idea


关闭元组参数'(key:_,value:_)'不支持
解构

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

代码:

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)
        }))
    }
}

此时出现错误尝试flatMap({(key,value)in

推荐答案

让我们从<$ c的定义开始$ c> flatMap 对于以下字典:

Let's start with the definition of flatMap for a dictionary which is the following:

func flatMap(_ transform: (Element) throws -> ElementOfResult?) rethrows -> [ElementOfResult]

你看到 transform closure只接受一个参数类型元素其中元素只是元组的 typealias

You see that the transform closure takes only one parameter of type Element where Element is just a typealias for a tuple:

public typealias Element = (key: Key, value: Value)

所以第一个和只有关闭的参数应该是两个元素的元组(类型类型价值)。

So the first and only argument of the closure should be a tuple of two elements (key of type Key and value of type Value).

现在,如果你看在您的代码中(在Swift 3中编译),您将看到情况并非如此,您应该问为什么这甚至可以在Swift 3中运行。

Now, if you look at your code (which compiles in Swift 3), you will see that this is not the case, and you should be asking why does this even work in Swift 3.

try flatMap({ (key, value) in
    return try transform(key, value)
})

您的闭包需要2个参数而不是1个(类型 Key 类型)。由于一个名为 destructuring 的功能,这在Swift 3中有效,编译器会自动将2个元素的元组转换为2个参数。

Your closure takes 2 arguments instead of one (key of type Key and value of type Value). This works in Swift 3 thanks to a feature called destructuring where the compiler will automatically transform a tuple of 2 elements into 2 arguments.

但是这个功能很奇怪,很少使用,并且在大多数情况下会产生意想不到的结果,所以它已经在Swift 4中删除了。

编辑:正如所指出的那样OOPer,此功能已在Swift 4测试版中暂时删除,但应在最终版本发布之前重新添加。

But this feature is weird, rarely used and gives unexpected results most of the time so it has been removed in Swift 4.
Edit: As pointed out by OOPer, this feature has been temporarily removed in Swift 4 beta but should be re-added before the final version is out.

相反,您应该写一下:

try flatMap({ tupleArgument in
    return try transform(tupleArgument.key, tupleArgument.value)
})

您的 flatMap 函数变为:

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

这篇关于关闭元组不支持Xcode 9 Swift 4中的解构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆