Cómo manejar correctamente el yo débil en bloques rápidos con argumentos
En miTextViewTableViewCell
, Tengo una variable para realizar un seguimiento de un bloque y un método de configuración donde el bloque se pasa y se asigna.
Aquí está miTextViewTableViewCell
clase:
//
// 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)
}
}
}
Cuando uso el método de configuración en micellForRowAtIndexPath
método, ¿cómo utilizo adecuadamente el yo débil en el bloque que paso?
Esto es lo que tengo sin el yo débil:
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
ACTUALIZAR: Tengo lo siguiente para trabajar usando[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
Cuando lo hago[unowned self]
en lugar de[weak self]
y sacar elif
declaración, la aplicación se bloquea. Alguna idea sobre cómo debería funcionar esto[unowned self]
?