[MacRuby-devel] My class's initialize not called

Brian Chapados chapbr at gmail.com
Thu Jan 21 18:41:58 PST 2010

Hi Steve (et al),

So, there are a couple things going on here.  Cocoa classes use the
concept of a "designated initializer" method, which is a name for the
instance method that calls the initializer method of the superclass.
This chain continues until you hit [NSObject#init]. To figure this out
from the documentation:

- If a class does not document an initializer method, check the
superclass, and continue up the chain until you find it.
- If there are multiple initializer methods, the documentation for
Cocoa classes will always tell you which method is the designated

In ObjC/Cocoa:
If you have a subclass that requires input during initialization, you
should create a new designated initializer.  That method should call
the designated initializer of the superclass and you should override
-init to call your new designated initializer.

All of this stuff is explained pretty well in Apple's Objective-C guide.

In Matt's example, he creates a few initialization helper methods, but
-init is still the designated initializer.

In your case, NSTextField is a subclass of NSControl, and the
designated initializer is (from NSControl):


I think your subclass of NSTextField should have an -initWithFrame:
method or another init method that calls super.initWithFrame:
Although, if you're not supposed to call super.init in MacRuby, then
I'm not sure how this is supposed to work.

Last point: The one odd exception case to the initialization chain is
the one you hit: NIB files!

nib/xib files are basically freeze-dried collections of objects that
are archived into a file.  When they are restored, this is done by
unarchiving the file, which uses the NSCoder methods. Most of the time
if you need to do custom configuration on an object instantiated from
a nib file, you should probably do that in -awakeFromNib.  However, if
for some reason you really need to do something during object
initialization, you need to do that in initWithCoder:, since this
method will be called for objects in a nib file.


On Thu, Jan 21, 2010 at 4:33 PM, steve ross <cwdinfo at gmail.com> wrote:
> So that confirms my suspicion. It's not so much a MacRuby thing as a Cocoa
> thing. I just don't see how to set an initial state in a class that's
> instantiated when a nib is loaded. I don't do the alloc.initWithWhatever. On
> the previous subject though, adding super/self does not cause the initialize
> method to be called. Hmmmmmm...
> On Jan 21, 2010, at 4:20 PM, Matt Aimonetti wrote:
> Overwriting the init method of a Cocoa class subclass is not recommended.
> Here is an example of what I was suggesting:
> http://github.com/mattetti/phileas_frog/blob/master/image_layer.rb#L44
> Which then get called
> there: http://github.com/mattetti/phileas_frog/blob/master/game_controller.rb#L57
> In any cases, you still need to call init (or super if you overwrite
> initialize) and you need to return self.
> _______________________________________________
> MacRuby-devel mailing list
> MacRuby-devel at lists.macosforge.org
> http://lists.macosforge.org/mailman/listinfo.cgi/macruby-devel

More information about the MacRuby-devel mailing list