Skip to content

Files

Latest commit

 

History

History
68 lines (53 loc) · 1.73 KB

self_in_property_initialization.md

File metadata and controls

68 lines (53 loc) · 1.73 KB

Pattern: Use of self in property initialization

Issue: -

Description

self refers to the unapplied NSObject.self() method, which is likely not expected. Make the variable lazy to be able to refer to the current instance or use ClassName.self.

Examples of correct code:

class View: UIView {
    let button: UIButton = {
        return UIButton()
    }()
}

class View: UIView {
    lazy var button: UIButton = {
        let button = UIButton()
        button.addTarget(self, action: #selector(didTapButton), for: .touchUpInside)
        return button
    }()
}

class View: UIView {
    var button: UIButton = {
        let button = UIButton()
        button.addTarget(otherObject, action: #selector(didTapButton), for: .touchUpInside)
        return button
    }()
}

class View: UIView {
    private let collectionView: UICollectionView = {
        let layout = UICollectionViewFlowLayout()
        let collectionView = UICollectionView(frame: .zero, collectionViewLayout: layout)
        collectionView.registerReusable(Cell.self)

        return collectionView
    }()
}

Examples of incorrect code:

class View: UIView {
    var button: UIButton = {
        let button = UIButton()
        button.addTarget(self, action: #selector(didTapButton), for: .touchUpInside)
        return button
    }()
}

class View: UIView {let button: UIButton = {
        let button = UIButton()
        button.addTarget(self, action: #selector(didTapButton), for: .touchUpInside)
        return button
    }()
}

Further Reading