Swift 3.0错误:转义闭包只能通过值显式捕获inout参数 [英] Swift 3.0 Error: Escaping closures can only capture inout parameters explicitly by value

查看:1005
本文介绍了Swift 3.0错误:转义闭包只能通过值显式捕获inout参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将我的项目更新为Swift 3.0但我遇到了一些困难。
我得到下一个错误:转义闭包只能通过值显式捕获inout参数。

I'm trying to update my project to Swift 3.0 but I have some difficulties. I'm getting next error: "Escaping closures can only capture inout parameters explicitly by value".

问题出在这个函数中:

fileprivate func collectAllAvailable(_ storage: inout [T], nextUrl: String, completion: @escaping CollectAllAvailableCompletion) {
    if let client = self.client {
        let _ : T? = client.collectionItems(nextUrl) {

            (resultCollection, error) -> Void in

            guard error == nil else {
                completion(nil, error)
                return
            }

            guard let resultCollection = resultCollection, let results = resultCollection.results else {
                completion(nil, NSError.unhandledError(ResultCollection.self))
                return
            }

            storage += results // Error: Escaping closures can only capture inout parameters explicitly by value

            if let nextUrlItr = resultCollection.links?.url(self.nextResourse) {

                self.collectAllAvailable(&storage, nextUrl: nextUrlItr, completion: completion) 
                // Error: Escaping closures can only capture inout parameters explicitly by value

            } else {
                completion(storage, nil) 
                // Error: Escaping closures can only capture inout parameters explicitly by value
            }
        }
    } else {
        completion(nil, NSError.unhandledError(ResultCollection.self))
    }
}

有人可以帮我解决这个问题吗?

Can someone help me to fix that?

推荐答案

专门为异步任务使用 inout 参数是滥用 inout - as调用函数时,调用者传递给 inout 参数的值不会改变。

Using an inout parameter exclusively for an asynchronous task is an abuse of inout – as when calling the function, the caller's value that is passed into the inout parameter will not be changed.

这是因为 inout 不是传递引用,它只是在函数退出时写回调用者的参数的可变阴影副本 - 并且因为异步函数立即退出,不会回写任何更改。

This is because inout isn't a pass-by-reference, it's just a mutable shadow copy of the parameter that's written back to the caller when the function exits – and because an asynchronous function exits immediately, no changes will be written back.

您可以在下面的Swift 2示例中看到这一点,其中 inout 允许从转义闭包中捕获参数:

You can see this in the following Swift 2 example, where an inout parameter is allowed to be captured by an escaping closure:

func foo(inout val: String, completion: (String) -> Void) {
    dispatch_async(dispatch_get_main_queue()) {
        val += "foo"
        completion(val)
    }
}

var str = "bar"
foo(&str) {
    print($0) // barfoo
    print(str) // bar
}
print(str) // bar

因为传递给的闭包是dispatch_async 转义函数 foo 的生命周期,对 val 所做的任何更改都不是写回调用者的 str - 只有在传递给完成函数时才能观察到更改。

Because the closure that is passed to dispatch_async escapes the lifetime of the function foo, any changes it makes to val aren't written back to the caller's str – the change is only observable from being passed into the completion function.

在Swift 3中, @escaping不再允许 inout 参数捕获闭包,这消除了期望传递引用的混乱。相反,您必须通过复制来捕获参数,方法是将其添加到闭包中捕获列表

In Swift 3, inout parameters are no longer allowed to be captured by @escaping closures, which eliminates the confusion of expecting a pass-by-reference. Instead you have to capture the parameter by copying it, by adding it to the closure's capture list:

func foo(val: inout String, completion: @escaping (String) -> Void) {
    DispatchQueue.main.async {[val] in // copies val
        var val = val // mutable copy of val
        val += "foo"
        completion(val)
    }
    // mutate val here, otherwise there's no point in it being inout
}

编辑:自发布此答案后, inout 参数现在可以编译为传递参考,可以通过查看发出的SIL或IR来看到。但是由于事实,你无法将它们视为那个c不能保证无论函数调用后,aller的值仍然有效。)

( Since posting this answer, inout parameters can now be compiled as a pass-by-reference, which can be seen by looking at the SIL or IR emitted. However you are unable to treat them as such due to the fact that there's no guarantee whatsoever that the caller's value will remain valid after the function call.)

但是,在你的情况下,根本不需要 INOUT 。您只需将请求中的结果数组附加到传递给每个请求的当前结果数组中。

However, in your case there's simply no need for an inout. You just need to append the resultant array from your request to the current array of results that you pass to each request.

例如:

fileprivate func collectAllAvailable(_ storage: [T], nextUrl: String, completion: @escaping CollectAllAvailableCompletion) {
    if let client = self.client {
        let _ : T? = client.collectionItems(nextUrl) { (resultCollection, error) -> Void in

            guard error == nil else {
                completion(nil, error)
                return
            }

            guard let resultCollection = resultCollection, let results = resultCollection.results else {
                completion(nil, NSError.unhandledError(ResultCollection.self))
                return
            }

            let storage = storage + results // copy storage, with results appended onto it.

            if let nextUrlItr = resultCollection.links?.url(self.nextResourse) {
                self.collectAllAvailable(storage, nextUrl: nextUrlItr, completion: completion) 
            } else {
                completion(storage, nil) 
            }
        }
    } else {
        completion(nil, NSError.unhandledError(ResultCollection.self))
    }
}

这篇关于Swift 3.0错误:转义闭包只能通过值显式捕获inout参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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