How do you allocate an object?
Answers
a. MyClass *obj = malloc(sizeof(MyClass));
b. MyClass *obj = [MyClass alloc];
c. MyClass *obj = alloc(MyClass);
d. MyClass *obj = [MyClass new];
e. None of the above.
I’m thinking b or may be d. What do you think?
This is just wrong. An objective-C object has a specific structure that malloc cannot make. This will NOT create a valid objective-C object.
This is the proper way to allocate an object. However all objective-c object must be passed some kind of -init message before it can be used.
No.
This is actually the same as [[MyClass alloc] init]. However since the user cannot choose which initializer to use when allocating the new object, most books recommending using the alloc/init method of creating a new object instead of this.