나는 것을 방지하려면 어떻게해야 는 스위프트 함수 포인터의주기를 유지 방지
는이
import UIKit
class MagicDataSource:NSObject,UITableViewDatasource {
deinit {
println("bye mds")
}
//cant use unowned or weak here
var decorator:((cell:CustomCell)->Void)?
func tableView(tableView:UITableView,cellForRowAtIndexPath indexPath:NSIndexPath)->UITableViewCell {
let cell = tableView.dequeueReusableCellWithIdentifier(Identifier, forIndexPath: indexPath) as CustomCell
decorator?(cell)
return cell
}
}
같은 데이터 소스 객체와 같은 뷰 컨트롤러가 상상 스위프트
에 개체로 기능 주위에 전달할 때주기를 계속 보유 당신은 뷰 컨트롤러를 폐기하는 경우, 데이터 소스가 해제하고도보기 통제 할 것되지 않습니다import UIKit
class ViewController: UIViewController {
var datasource:MagicDataSource? = MagicDataSource()
deinit {
println("bye ViewCon")
}
override func viewDidLoad() {
super.viewDidLoad()
datasource?.decorator = decorateThatThing
}
func decorateThatThing(cell:CustomCell) {
//neither of these two are valid
//[unowned self] (cell:CustomCell) in
//[weak self] (cell:CustomCell) in
cell.theLabel.text = "woot"
}
}
해당 개체에 대한 강한 심판이있다 (그리고 원)하는 그것은보기 컨트롤러에 decorateThatThing
함수에 대한 강한 참조를 보유하고 있습니다.
당신은주기를 중지하고 ViewController
에서이 작업을 수행하여 해제 할 수있는 장식을 얻을 수 있지만, 지저분한 느낌 수
override func viewWillDisappear(animated: Bool) {
super.viewWillDisappear(animated)
datasource?.decorator = nil
}
override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
datasource?.decorator = decorateThatThing
}
그래서 질문은 내가를 해체하는 것을 방지하기 위해 및/또는 기능 바르를 선언 어떻게입니다 뷰 컨트롤러가 삭제 될 때 연관된 데이터 소스도 해제되도록 수동으로 데이터 소스를 설정하십시오.
않아요 작업을 사용할 수 있습니다. 내가보기 컨트롤러가 데이터 소스를 소유하고 싶습니다. 약한 경우 인스턴스화시 버려집니다 –