Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Rodrigo Chousal
16,009 PointsDesignated Initializers and Convenience Constructors
Declare a method which is a convenience constructor for the class Song. The method calls a designated initializer named: initWithSinger: (NSString*) singer
@interface Song : NSObject
+ ( id ) initWithSinger: (NSString*) singer;
+ ( id ) _________: (NSString*) singer;
Could someone give me the answer and provide a brief explanation as to how you got it?
Thank you!
1 Answer
Misha Shaposhnikov
8,718 PointsA designated initializer is used to initialize an object with some data that you already know needs to be in that object, so you just pass it into the initializer so that the data you want is already in the object; it does not, however, allocate memory for the object, and as such, the initializer gets called on an allocated instance of the object. Memory still has to be allocated by calling alloc. A convenience constructor allocates memory and initializes an object all in one method call.
The naming convention for a designated initializer is the word "init" followed by the word "With" and the argument names. The convention for convenience constructors is just the class name followed by the word "With" and the argument names.
Here are the declarations:
/* .h file */
@interface Song : NSObject
+ ( id ) initWithSinger: (NSString * ) singer;
+ ( id ) songWithSinger: (NSString *) singer;
Happy Coding!