【发布时间】:2018-12-13 12:14:43
【问题描述】:
我在设置UIAlertController 时出现内存泄漏,我看到其他线程在UIAlertController 中谈论内存泄漏。但我不知道如何更改我的代码,以便内存泄漏消失。我将itemSelected 从函数更改为计算属性,但它没有改变任何东西。
protocol TriggerUIAlertController: class where Self: UIView {
var itemsForPresenting: [String] { get }
var titleForCancel: String { get }
var titleForAlertController: String { get }
var itemSelected: Int? {get set}
}
extension TriggerUIAlertController {
func triggerUIAlerController() {
let alertList = UIAlertController(title: titleForAlertController, message: nil, preferredStyle: .actionSheet)
let closure = { (alert: UIAlertAction!) -> Void in
let index = alertList.actions.index(of: alert)
guard index != nil else {
return
}
///produces memory leak, idk why though -> has to be checked
self.itemSelected = index!
}
for x in itemsForPresenting {
alertList.addAction(UIAlertAction(title: x, style: .default, handler: closure))
}
self.window?.rootViewController?.present(alertList,animated: true, completion: nil)
let cancelAction = UIAlertAction(title: titleForCancel, style: .cancel, handler: nil)
alertList.addAction(cancelAction)
}
}
顺便说一句:仪器在使用大约 5 分钟后使用总共 50gb ram 是否正常?
【问题讨论】:
-
说到 Instruments 内存使用 - 5gb 是正常的,50 太多了。
标签: swift memory-leaks protocols uialertcontroller