简体   繁体   English

swift 3 中的@escaping 闭包函数

[英]@escaping closure function in swift 3

Error with this below function Closure use of non-escaping parameter 'completion' may allow it to escape下面这个函数的错误Closure use of non-escaping parameter 'completion' may allow it to escape

func retrieveCannedRecommendedEntities() -> Future<CannedRecommendedEntities, NSError> {
  return Future() { completion in
    self.retrieve(.onboarding) { response in
      switch response {
      case .success(let val):
        let payload: AnyObject = val.value.json! as AnyObject
        let json = JSON(payload)

        guard let suggestions = self.parseEntitiesFromJSON(json, atKey: "suggestion") else {
          completion(SqorResult.error(self.parsingError))
        }

        let teams = suggestions.filter {
          $0.entityType != .Player
        }

        let athletes = suggestions.filter {
          $0.entityType == .Player
        }

        completion(SqorResult.success(Box((teams, athletes))))

      case .error(let error):
        completion(SqorResult.error(error))
      }
    }
  }
}

A closure is said to escape a function when the closure is passed as an argument to the function, but is called after the function returns.当闭包作为参数传递给函数时,闭包被称为对函数进行转义,但在函数返回后被调用。

If you use for closure asynchrony, mean the closure may be called after the function execution, you need to add @escaping .如果用于闭包异步,意味着闭包可能会在函数执行后被调用,则需要添加@escaping

Even if you have no error compilation, you might have run time error, because the closure memory can be released.即使你没有错误编译,你也可能有运行时错误,因为可以释放闭包内存。

@escaping assure you to prevent your closure. @escaping向您保证防止关闭。

The compiler error means that the parameter completion in function Future - which is a closure - is specified as "non-escaping" (this is the default).编译器错误意味着函数Future中的参数completion - 这是一个闭包 - 被指定为“非转义”(这是默认值)。 This means, that the closure completion must be executed before function Future returns.这意味着,必须函数Future返回之前执行闭包completion

However, the implementation indicates that completion may "escape" the function Future - which means, that completion may be executed after function Future returns.然而,实现表明completion可以“逃避”函数Future - 这意味着completion可以函数Future返回执行。

In order to fix this programmer error, you need to ensure that the closure completion will be executed before function Future returns, or you need to add the modifier @escaping to the parameter completion .为了修复这个程序员错误,您需要确保函数Future返回之前执行闭包completion ,或者您需要在参数completion添加修饰符@escaping

See also Escaping Closures另见转义闭包

Note, that both solutions may have implications, because they either need a change of the API of a function possibly defined in an existing library (eg "Future") or it is incompatible with your use-case, since you call completion in another - possibly escaping - closure which is set as a parameter in self.retrieve .请注意,这两种解决方案都可能有影响,因为它们要么需要更改现有库(例如“Future”)中可能定义的函数的 API,要么与您的用例不兼容,因为您在另一个中调用了completion -可能转义 - 闭包,它被设置为self.retrieve的参数。

However, the notion "completion" in the context of a "Future" clearly dictates, that completion should be "escaping".然而,“未来”上下文中的“完成”概念清楚地表明, completion应该是“逃避”。 So, adding @escaping to the function signature of Future seems to be to way to go.因此,将@escaping添加到Future的函数签名似乎是要走的路。

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

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