Mutating self (struct/enum) inside escaping closure in Swift 3.0

后端 未结 3 814
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-01-04 08:07

In swift 2.2, We could mutate a struct or enum within a closure, when it was inside a mutating function. But in swift 3.0 its no longer possible. I get the following error

3条回答
  •  礼貌的吻别
    2021-01-04 08:40

    The problem is that @escaping closures can be stored for later execution:

    Escaping Closures

    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. ...

    One way that a closure can escape is by being stored in a variable that is defined outside the function....

    Since the closure can be stored and live outside the scope of the function, the struct/enum inside the closure (self) will be copied (it is a value) as a parameter of the closure. And, if it was allowed to mutate, the closure could have an old copy of it, causing unwanted results.

    So, in answer to your question, you cannot; unless you are able to remove "@escaping" (not your case because it's a 3rd party API)

提交回复
热议问题