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

Originator:mail.ostapa
Number:rdar://29389821 Date Originated:25.11.2016
Status:Open Resolved:
Product: Product Version:iOS 10
Classification: Reproducible:Always
 
According to Apple documentation initialize() method Initializes the class before it receives its first message.

initialize() is not called before calling class method in Release build configuration

For Objective-C project in both Debug and Release configurations calling a class method is enough to trigger + initialize.

Steps to Reproduce:
1. Create new Swift iOS project in Xcode 8 
2. Add following code

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

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

3. Change build configuration to Release
4. Run project

Expected Results:
Console output:
initialize
test


Actual Results:
Console output:
test

Comments

probably expected behavior?

I'm thinking this may actually be behaving properly, depending on Apple's definition of message sending. Observe what happens with the following code:

Test.test()
let a = Test()
Test.test()

Output:

test
initialize
test

So you can see that initialize is in fact called in release, but only when the first instance is created. If you're going for a singleton pattern, you could do so with the following, just be aware that this is also lazily executed when Test.instance is first used:

class Test: NSObject {
     static let instance = Test()
}

Please note: Reports posted here will not necessarily be seen by Apple. All problems should be submitted at bugreport.apple.com before they are posted here. Please only post information for Radars that you have filed yourself, and please do not include Apple confidential information in your posts. Thank you!