Not getting "requires explicit use of 'self' to make capture semantics explicit" errors in Xcode, build succeeds when it should fail

huangapple go评论73阅读模式
英文:

Not getting "requires explicit use of 'self' to make capture semantics explicit" errors in Xcode, build succeeds when it should fail

问题

我有一个 Xcode 项目,其中在需要的闭包中没有使用 `self.`。

    class Example {
        func bar() {   
            DispatchQueue.main.async { [weak self] in
                guard let self = self else {
                    return
                }

                print(foo) // 应该是 print(self.foo),但在这里没有报错
            }
        }
        
        var foo: String {
            "foo"
        }
    }

尽管如此,构建成功了。我期望出现以下错误:

> 在闭包中引用属性 'foo' 需要明确使用 'self' 以使捕获语义明确

我的环境:Swift 5.8.1/Xcode 14.3.1
英文:

I have an Xcode project that doesn't use self. inside of a closure where it's needed.

class Example {
    func bar() {   
        DispatchQueue.main.async { [weak self] in
            guard let self = self else {
                return
            }

            print(foo) // should be print(self.foo), but not getting an error here
        }
    }
    
    var foo: String {
        "foo"
    }
}

Despite this, the build succeeds. I expect the following error to appear:

> Reference to property 'foo' in closure requires explicit use of 'self' to make capture semantics explicit

My environment: Swift 5.8.1/Xcode 14.3.1

答案1

得分: 3

这在使用 Xcode 14.3 • Swift 5.8 或更新版本时是正确的。请查看 Swift 进化的 在 self 解包后允许隐式 self 弱引用捕获

英文:

This is correct if you are using Xcode 14.3 • Swift 5.8 or later. Take a look at Swift evolution's Allow implicit self for weak self captures, after self is unwrapped.

huangapple
  • 本文由 发表于 2023年7月11日 10:14:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/76658337.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定