我有一个父类和一个子类。父项对子项有强引用,子项对父项有无主引用。在 parent 的 deinit 期间,我希望 child 做一些清理工作,这涉及调用 parent:
class ViewController: UIViewController
{
override func viewDidLoad()
{
super.viewDidLoad()
let parent = Parent()
}
}
class Parent : NSObject
{
override init()
{
super.init()
child.doStuff()
}
deinit
{
child.doStuff()
}
lazy private var child : Child = Child(parent : self)
}
class Child : NSObject
{
init(parent : NSObject)
{
self.parent = parent
}
func doStuff()
{
println(self.parent)
}
deinit
{
}
private unowned var parent : NSObject
}
不幸的是,在父级的 deinit 期间调用 doStuff()
会导致崩溃,因为它使用 self.parent
:
libswiftCore.dylib`_swift_abortRetainUnowned:
0x111e91740 <+0>: leaq 0x1e6cd(%rip), %rax ; "attempted to retain deallocated object"
0x111e91747 <+7>: movq %rax, 0x58612(%rip) ; gCRAnnotations + 8
0x111e9174e <+14>: int3
-> 0x111e9174f <+15>: nop
据我了解,parent应该还存在,因为parent的deinit还没有完成。然而,此错误似乎表明子级无法再访问其对父级的 unowned
引用。
任何人都可以阐明这一点吗?
最佳答案
在这种情况下,unowned(unsafe) 会这样做。 但我个人不会将 unowned(unsafe) 用于桥接 objective-c 代码以外的任何用途。
如果可能的话,我会尽量避免从 deinit() 调用 child.doStuff() 的需要。我遇到过类似的情况,我只是添加了一个 .unload() 方法,我自己的代码负责在需要时调用该方法。在上面的例子中,ViewController 可以接受这个责任。
我想乌托邦式的解决方案是,在可能的情况下/如果可能的话,找到一种方法使对象在设计上不会如此交织在一起。
unload() 场景示例:(我在终端 repl 中测试了它,因此没有 UIKit)
import Foundation
class ViewController {
let parent = Parent()
deinit {
parent.unload()
}
}
class Parent {
init() {
child.doStuff()
}
func unload() {
// Code used to be in deinit
child.doStuff()
}
lazy private var child : Child = Child(parent : self)
}
class Child {
init(parent : Parent) {
self.parent = parent
}
func doStuff() {
println(self.parent)
}
private unowned var parent : Parent
}
var vc:ViewController? = ViewController()
vc = nil
关于swift - 子对象可以在父对象的 deinit 期间引用其父对象吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30524494/