使用无主引用或闭包捕获列表来避免Swift引用循环。
示例代码:
class MyClass { var stringProperty: String { return "Hello, world!" }
func myMethod() {
// create a closure that captures stringProperty
let closure = { [unowned self] in
print(self.stringProperty)
}
// call the closure
closure()
}
}
let myInstance = MyClass() myInstance.myMethod() // prints "Hello, world!"