0

If I am writing my own custom init method what would I call be the appropriate call to super init be? I sometimes see it's not just some super init method but something else, what's the general rule for this?

4 Answers 4

2

Objective-C classes may have multiple init methods. Usually, one of them is called the "Designated Initializer", and is the one all the rest call.

If you are subclassing, and creating an init method with a different signature, you should call the superclass' designated initializer (although calling any initializer of the superclass will work as well). The documentation for the classes will usually tell you what the designated initializer is.

For instance, in UITableViewCell the designated initializer is initWithStyle:reuseIdentifier: and is the one you should call if you subclass and create an init method with a different signature.

Sign up to request clarification or add additional context in comments.

1 Comment

I think its better practice to call the designated initializer on self, not super, unless you're coding a simple initializer called "init". Hence initWithBlah: should call "self = [self init]" rather than "self = [super init]". This way if you ever do decide to override the designated initializer you'll be ready
0

It depends on the context, try to put in some nslogs and see which init is called for you. If it is [super init] or something like initWithNib...

2 Comments

so it depends in which super init is called? what if I have two inits in the super.. I won't know which ones which is called?
try to add both (id)init and (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil to your class if they are not present. Insert a NSLog in both methods and see which one is called by your code when it gets initialized now.
0

Building your own init method, you should call super init, or if your object is associated with a xib, you could also call super initWithNibName:bundle: as a shorcut to force the one that implement your object to use one and only one xib. In that last case, you should also overload initWithNibName:bundle: to make the same force call in case the caller uses it.

If your object is included in a XIB, also overload initWithCoder

2 Comments

what does an object is included in XIB, you mean if I subclass a UILabel and UILabel is on thexib?
@xonegirlz : Yes, that's what I mean. Anyone can also put any non graphical object into a xib (I'm not taking about the view into the xib, but the xib itself), changing its class attribute to reference a NSString or anything else, and link it to an IBOutlet to instanciate the object when instanciating the XIB.
0

The general rule is you should call [super init] when you initialize your class. That way the init method of the class you inherit of (typically NSObject) will also be called and thus constructed. If all classes follow this rule a chain of inits will be established.

Same rule goes in dealloc, there you call the [super dealloc].

1 Comment

that's not a good rule. The general rule is to call the designated initializer of the super class. If your class inherits from UIView, for example you should call [super initWithFrame:].

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.