英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论