johnno1962 / injectionforxcode

Runtime Code Injection for Objective-C & Swift
MIT License
6.55k stars 565 forks source link

Fields not injected #179

Closed ghost closed 7 years ago

ghost commented 7 years ago

I have this class in one file

class Theme : NSObject {
    var colorA = UIColor.blue

    var colorB : UIColor {
        return UIColor.blue
    }

    var colorC : UIColor = {
        print("!")
        return UIColor.blue
    }()

    var colorD : UIColor

    lazy var colorE : UIColor = {
        print("!")
        return UIColor.blue
    }()

    lazy var colorF = UIColor.blue

    override init() {
        colorD = UIColor.blue
        super.init()
    }
}

and in another:

class ViewController: UIViewController {
    func injected() {
        let theme = Theme()
        print("A", theme.colorA)
        print("B", theme.colorB)
        print("C", theme.colorC)
        print("D", theme.colorD)
        print("E", theme.colorE)
        print("F", theme.colorF)
    }
}

Changing color from blue to something else, and injecting, seems to affect only B, E, and F variables. I wonder why? C and D are especially strange, because methods that set them are called.

johnno1962 commented 7 years ago

Treatment of vars in Swift is asking a lot of injection particularly for Swift. Best stick to changes to the implementation of methods which is what it is designed for.