【发布时间】:2017-03-13 08:12:54
【问题描述】:
请考虑以下代码:
class Module {
let viewController = ExampleViewControler()
deinit {
print("deinit")
}
}
class ExampleViewControler: UIViewController {}
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {
var window: UIWindow?
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
window = UIWindow(frame: UIScreen.main.bounds)
let mod = Module()
let navController = UINavigationController(rootViewController: mod.viewController)
window?.rootViewController = navController
window?.makeKeyAndVisible()
return true
}
viewController 由 navController 保留。为什么在 Module 实例上调用 deinit?既然它的属性被保留了,它不应该也被保留吗?
无论如何,我想让对象保持活跃,因为它的视图控制器还活着。我怎样才能做到这一点?
谢谢!
【问题讨论】:
标签: ios swift memory-management dealloc retain-cycle