Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSObject's initialize() not called in Release build configuration

According to Apple documentation initialize() method Initializes the class before it receives its first message.

Can somebody explain why initialize() is not working in Release build configuration?

For example:

class Test: NSObject {
    override class func initialize() {
        print("initialize")
    }
    class func test() {
        print("test")
    }
}

class ViewController: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
        Test.test()
    }
}

Output in Debug configuration:

initialize
test

Output in Release configuration:

test
like image 847
Ostap Horbach Avatar asked Nov 24 '16 10:11

Ostap Horbach


1 Answers

I did a quick test and it looks like in Release configuration + initialize is not called unless you create an instance of the class. However in Debug calling a class method is enough to trigger +initialize. Looks like an undocumented caveat.

Edit: Even more interesting fact is that for Objective-C project in both Debug and Release configurations calling a class method is enough to trigger + initialize. I would say this is a bug. You might want to file a radar for it.

like image 84
Adam Avatar answered Nov 15 '22 05:11

Adam