【发布时间】:2018-05-13 01:30:13
【问题描述】:
在我的 iOS 应用中
class Node {
var value: String
var isExpanded: Bool
var children: [Node] = []
private var flattenElementsCache: [Node]!
// init methods
var flattenElements: [Node] {
if let cache = flattenElementsCache {
return cache
}
flattenElementsCache = []
flattenElementsCache.append(self) // (1) <-- Retain Cycle???
if isExpanded {
for child in children {
flattenElementsCache.append(contentsOf: child.flattenElements)
}
}
return flattenElementsCache;
}
}
使用 Instruments,我观察到一些内存泄漏,我认为问题符合 (1) 所示。
如果它产生了一个保留周期,谁能向我解释一下?如果是,您如何解决?
【问题讨论】:
标签: ios swift memory-leaks retain-cycle