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 trialRohan A.
4,008 PointsConstructors and Destructors in Objective-C?
Many blog posts claim that there are no constructors and destructors in Objective-C. Is it an appropriate statement?
We always have an object initialized by calling the init method immediately after itβs allocated. This is why instantiation is always a two-step process: allocate, then initialize -
self.myTitle = [[NSArray alloc] initWithObjects:@"Hello", @"World", nil];
Treehouse explained convenience constructors very nicely. But, is it actually different from "constructors"?
self.myTitle = [NSArray arrayWithObjects: @"Hello",@"World", nil];
Any destructor example? (we no longer use dealloc, so can't think of anything else).
2 Answers
Amit Bijlani
Treehouse Guest TeacherTraditionally in languages like Java and C# a constructor is where you would perform your initializations. In Objective-C you would do so in the init
method even though you create a convenience constructor. The convenience constructor as the name suggests is a shortcut so you don't have to write out two statements namely: alloc and init.
Prior to Automatic Reference Counting you would have to perform all your object deallocations in the dealloc
method. Now thanks to ARC you no longer have to worry about manually deallocating objects. For the rare occasions that you do need to perform any manual housekeeping you can override the dealloc
method but then do not forget to make a call to [super dealloc]
.
Here's a word of caution directly from the docs:
Even though ARC destroys instance variables automatically, there are still legitimate reasons to write a dealloc method, such as freeing non-retainable resources. Failing to call [super dealloc] in such a method is nearly always a bug. Sometimes, the object is simply trying to prevent itself from being destroyed, but dealloc is really far too late for the object to be raising such objections. Somewhat more legitimately, an object may have been pool-allocated and should not be deallocated with free; for now, this can only be supported with a dealloc implementation outside of ARC. Such an implementation must be very careful to do all the other work that NSObjectβs dealloc would, which is outside the scope of this document to describe.
Stefan Jansen
8,974 PointsI think that the statement is correct. You allocate memory and than instantiate the object. You could see the instantiate method as a sort of constructor, but you really shouldn't. It can only cause more confusion learning objective-c (I know, I'm a PHP programmer as well). In special occasions you could overwrite the dealloc method, but I can't think of any.
Can you give an example where you would like a "destructor"?
Rohan A.
4,008 PointsRohan A.
4,008 PointsAmit Bijlani I would appreciate your thoughts on this question.