簡體   English   中英

iOS - 弱變量仍然會導致保留周期?

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM