Skip to content

Latest commit

 

History

History
64 lines (58 loc) · 1.13 KB

README.md

File metadata and controls

64 lines (58 loc) · 1.13 KB

RetainCycleExample

An example for Swift retain cycle

RetainCycleExample

Issue

class BankAccount {
    var owner: Owner?
    init() {
        
    }
}
class Owner {
    var account: BankAccount?
    init() {
        
    }
}

let bankAccount = BankAccount()
let owner = Owner()
bankAccount.owner = owner
owner.account = bankAccount

Fix

var owner: Owner?

↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓

weak var owner: Owner?

RetainCycleExample2

Issue

lazy var configureCell: (UITableViewCell)->() = { cell in
    if let tableViewCell = cell as? TableViewCell {
        tableViewCell.title?.text = self.name
    }
}

Fix

lazy var configureCell: (UITableViewCell)->() = { cell in

↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓

lazy var configureCell: (UITableViewCell)->() = { [unowned self] cell in

RetainCycleExample3

Issue

var delegate: TableViewCellDelegate?

Fix

var delegate: TableViewCellDelegate?

↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓

weak var delegate: TableViewCellDelegate?