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 trialAlex Atwater
2,624 Pointssuper init method
in this video: http://teamtreehouse.com/library/build-a-blog-reader-iphone-app-2/data-modeling/designated-initializers-and-convenience-constructors-2 at around 7:50, Amit Bijlani did this:
-(id) iniWithTitle(NSString *)title {
self = [super init]
}
so he is in the class of BlogPost, yet as far as i know, which is why i'm confused, he is initializing it with the super class, which is NSObject. So if he initializes self, an instance of BlogPost with the NSObject initializer, a) isn't it an instance of NSObject and b) if it is an NSObject, how can it access self.title if self.title is a property of BlogPost if its not initialized that way?
2 Answers
Amit Bijlani
Treehouse Guest TeacherThat is a really good question and has a very complicated answer which can be found in this article: http://www.cocoawithlove.com/2009/04/what-does-it-mean-when-you-assign-super.html
John W
21,558 PointsObjective-C's alloc-init pattern, which handles alloc and init separately, is not to be confused with C++ or Java's constructor pattern, which conceptually bundles both alloc and init in one call.
At the high level, the allocation of the required properties and ivars are done and made available during alloc
. The purpose of init
is to initialize everything such as variables with initial values. Your superclass's init
initializes everything common among all subclasses, but it is still allocated and synthesized as your subclass in the part where you called [YourClass alloc]
. Hence if you do nothing in your own init
but [super init]
, your own properties such as title will be available since they are allocated by alloc, but they won't be initialized and hold the default value nil
Stone Preston
42,016 PointsStone Preston
42,016 PointsThat is some difficult stuff to understand right there. Anyway you could explain it in simpler terms Amit Bijlani