Skip to content

kidd0717/RetainCycleExample

Repository files navigation

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?

Releases

No releases published

Packages

No packages published

Languages