[英]iOS - Weak var can still cause retain cycle?
這是我的真實代碼:
@IBOutlet weak var contentTextView: SmartTextView! {
didSet {
self.contentTextView.onDidBeginEditing = {
$0.layer.borderColor = Util.green.CGColor
}
self.contentTextView.onDidEndEditing = {
$0.layer.borderColor = Util.gray.CGColor
}
self.contentTextView.layer.borderWidth = 1 / Util.screenScale
self.contentTextView.layer.borderColor = Util.gray.CGColor
self.contentTextView.minHeight = 148
self.contentTextView.maxHeight = 148
self.contentTextView.onChange = { [unowned self] text in
var content = text.stringByTrimmingCharactersInSet(NSCharacterSet(charactersInString: "\n\t"))
self.contentLenthLabel.text = "\(self.MAX_CONTENT - count(content))"
}
}
}
如果我刪除[unowned self]
語句,我會在 Instruments 中看到一個保留循環問題。
KVO 或其他什么使弱 var 仍然會導致保留循環?
weak
引用是一個紅鯡魚; 它與這里的故事無關。 沒有[unowned self]
,你就保留了這個觀點,這個觀點也保留了你。 這是一個保留周期:
UIViewController 保留其視圖
視圖保留其子視圖; 這些子視圖之一是 SmartTextView
SmartTextView 保留了onChange
功能
除非你說unowned self
否則函數會保留self
(UIViewController)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.