Cocoa subclass initialization


When subclassing a Cocoa class, such as NSWindowController, what is the recommended initialization procedure in RM? When using the regular Ruby initialize method with optional arguments, it doesn’t get called at all by new unless at least one argument is specified. For Cocoa (sub)classes, is using alloc.initWhatever and calling super still the way to go? What is the difference in the way new/initialize works (or doesn’t) with Cocoa?



First, come join us on the RubyMotion Slack, we have a Mac OSX channel.

Cocoa want you to cal the ‘preferred’ initializer. To make sure you are calling the correct one, the X.alloc.initWhatever method works best. Calling will call the X.alloc.init so you may not be getting everything initialized correctly.

Personally I like to call on pure Ruby object (models and things like that, stuff that doesn’t subclass anything in AppKit) and use the Apple preferred method on NSWindowController, and so forth (including any subclasses of AppKit I have created).


I keep an eye on Slack, my work schedule these days just doesn’t let me stay there long.

I was trying to create a general-purpose initializer, but I see that the new method is different depending on whether it is a Ruby or a Cocoa class. Now I just need to work out some custom init methods - thanks!