Como manipular corretamente o Self fraco em blocos rápidos com argumentos
No meuTextViewTableViewCell
, Eu tenho uma variável para acompanhar um bloco e um método de configuração em que o bloco é passado e atribuído.
Aqui está o meuTextViewTableViewCell
classe:
//
// TextViewTableViewCell.swift
//
import UIKit
class TextViewTableViewCell: UITableViewCell, UITextViewDelegate {
@IBOutlet var textView : UITextView
var onTextViewEditClosure : ((text : String) -> Void)?
func configure(#text: String?, onTextEdit : ((text : String) -> Void)) {
onTextViewEditClosure = onTextEdit
textView.delegate = self
textView.text = text
}
// #pragma mark - Text View Delegate
func textViewDidEndEditing(textView: UITextView!) {
if onTextViewEditClosure {
onTextViewEditClosure!(text: textView.text)
}
}
}
Quando uso o método configure no meucellForRowAtIndexPath
método, como uso adequadamente o eu fraco no bloco em que passo.
Aqui está o que eu tenho sem o eu fraco:
let myCell = tableView.dequeueReusableCellWithIdentifier(textViewCellIdenfitier) as TextViewTableViewCell
myCell.configure(text: body, onTextEdit: {(text: String) in
// THIS SELF NEEDS TO BE WEAK
self.body = text
})
cell = bodyCell
ATUALIZAR: Trabalhei usando o seguinte[weak self]
:
let myCell = tableView.dequeueReusableCellWithIdentifier(textViewCellIdenfitier) as TextViewTableViewCell
myCell.configure(text: body, onTextEdit: {[weak self] (text: String) in
if let strongSelf = self {
strongSelf.body = text
}
})
cell = myCell
Quando eu faço[unowned self]
ao invés de[weak self]
e retire oif
declaração, o aplicativo falha. Alguma idéia de como isso deve funcionar?[unowned self]
?