简体   繁体   English

错误:Escaping 闭包捕获变异的“自我”参数

[英]Error: Escaping closure captures mutating 'self' parameter

I've reviewed the other questions on this topic on SO (there are many) but none of them address this error pertaining to a @State .我已经在 SO 上查看了有关此主题的其他问题(有很多),但没有一个解决与@State相关的此错误。 And I thought that this was the very problem that @State solved!我认为这正是@State解决的问题!

Here is the relevant code这是相关代码

 struct ViewA: View {
    @State private var startAnimation = false
    
    let width: CGFloat
    let height: CGFloat
    
    init(width: CGFloat, height: CGFloat) {
        self.width = width
        self.height = height
        
        DispatchQueue.main.asyncAfter(deadline: .now() + 1.0) {
            
            startAnimation = true //<--- The error is on this line
        }
            
        
    }
    
    var body: some View {
        Circle()
            .frame(width: width, height: height)
            .scaleEffect(CGSize(width: startAnimation ? 5.0 : 1.0 , height: startAnimation ? 5.0 : 1.0))
    }
}

There error is Escaping closure captures mutating 'self' parameter有错误是Escaping closure captures mutating 'self' parameter

Now reviewing the other SO questions related to this error lets me know that this error is related to time delayed changes ...现在查看与此错误相关的其他 SO 问题让我知道此错误与时间延迟更改有关...

I don't think it has anything to do with the @State property, but with the fact that you are using an @escaping closure.我认为这与 @State 属性没有任何关系,但与您使用的是 @escaping 闭包这一事实有关。 An escaping closure can cause a strong reference cycle if you use self inside the closure.如果在闭包中使用 self ,则 escaping 闭包可能会导致强引用循环。 See for a nice article explaining @escaping closures this link .请参阅此链接中解释 @escaping 闭包的精彩文章。

Kind regards, MacUserT亲切的问候, MacUserT

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

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