镝是 E-sites iOS 套件 的一部分。
运行时对象的释放辅助器。
此库仅用于调试目的。
安装
##Swift PM
package.swift 依赖
.package(url: "https://github.com/e-sites/dysprosium.git", from: "6.0.0"),
并将 "Dysprosium" 添加到您的应用/库目标的 dependencies
中,例如:
.target(name: "BestExampleApp", dependencies: ["Dysprosium", "DysprosiumLogger"]),
实现
// AppDelegate
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
// ...
Dysprosium.shared.onDealloc { objects in
print("[INFO] Deallocated objects: \(objects)")
}
Dysprosium.shared.onExpectedDeallocation { object, message in
print("[WARNING] Expected deallocation of \(object): \(message)")
}
}
日志释放
这样可以轻松地看到哪些对象已被释放。
import Dysprosium
class SomeObject: DysprosiumCompatible {
// ...
deinit {
deallocated()
}
}
期望释放
UIViewController
大多数情况下,当UIViewController
消失时,应该将其释放。
使用expectDeallocation()
可以监控它是否实际被释放。
import Dysprosium
class SomeViewController: UIViewController, DysprosiumCompatible {
// ...
override func viewDidDisappear(_ animated: Bool) {
super.viewDidDisappear(animated)
expectDeallocation()
}
deinit {
deallocated()
}
}
相关对象
如果对象被释放,但你需要确认底层对象也被释放
import Dysprosium
class SomeObject: DysprosiumCompatible {
// ...
let relatedObject: SomeObject
deinit {
relatedObject.expectDeallocation()
deallocated()
}
}
发布构建
像这样禁用Dysprosium
Dysprosium.shared.isEnabled = false