Edward Anthony
Edward Anthony

Reputation: 3464

What does init in Objective-C actually do?

+ (id)alloc;

and

- (id)init;

are methods from NSObject.h

The alloc does

+ (id)alloc {
    return _objc_rootAlloc(self);
}


id
_objc_rootAlloc(Class cls)
{
#if 0  &&  __OBJC2__
    // Skip over the +allocWithZone: call if the class doesn't override it.
    // fixme not - this breaks ObjectAlloc
    if (! ((class_t *)cls)->isa->hasCustomAWZ()) {
        return class_createInstance(cls, 0);
    }
#endif
    return [cls allocWithZone: nil];
}

It does memory allocation, and return a class Instance.

But when I came to the init method, this is the implementation

- (id)init {
    return _objc_rootInit(self);
}

id
_objc_rootInit(id obj)
{
    // In practice, it will be hard to rely on this function.
    // Many classes do not properly chain -init calls.
    return obj;
}

It only return self object (NSObject) without doing any initialization.

The documentation also says the same thing.

"The init method defined in the NSObject class does no initialization; it simply returns self."

If that is the case, alloc method alone is sufficient. Init method is only required for overridding.

Any explanation here?


This is the implementation source NSObject.mm

http://www.opensource.apple.com/source/objc4/objc4-532/runtime/NSObject.mm

Upvotes: 4

Views: 397

Answers (2)

Duncan C
Duncan C

Reputation: 131426

CRD explained it pretty well.

I will state it more strongly, however.

The way you create a new object in Objective C is to use alloc/init.

You should always initialize every object, and custom init methods should always call [super init]. Consider failure to call init on ALL objects an error, and also consider calling [super init] in your custom init methods an error.

Upvotes: 0

CRD
CRD

Reputation: 53000

alloc is to do with memory allocation while init (or initX etc., the init family) is to do with configuring that allocated memory as needed when an object is created - whether any particular class, now or in the future following some revision, needs to do any work in init is dependent on the semantics of that class. However as you don't know for any arbitrary class whether it's init needs to do any work you must call it, and as any arbitrary class does not know whether its superclass needs to do any initialisation to must call its superclasses init within its own init. For this chain to work NSObject must have an init, it so happens that it (currently, who knows in the future) does no work. NSObject's init is the end of the chain and the only one that does not need to call another init.

Confusion arises for some as many languages combine the two operations, allocation and initialisation, into one indivisible operation, e.g. new in Java. Indeed Cocoa has a new as well which is defined as alloc followed by init.

And Apple should really have written:

The init method defined in the NSObject class currently does no initialization; it simply returns self.

or simply said nothing.

HTH

Upvotes: 5

Related Questions