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
Gary Luce
7,153 PointsLost and designated initializers
Hey,
I finished the iOS learning adventure and I am now reading the big nerd ranch guide, everything is sort of going smoothly except for designated initializers and convenience constructors.
I understand the point of allocating memory for an object and initializing it. I understand that convenience constructors are just a way to init while passing an argument, but I'm lost when it comes down to this type of stuff:
- (void)setContainer:(BNRItem *)i;
- (BNRItem *)container;
- (void)setItemName:(NSString *)str;
- (NSString *)itemName;
- (void)setSerialNumber:(NSString *)str;
- (NSString *)serialNumber;
I understand that void is a way to set the instance but without a value, I guess? and then I create a setContainer method (is this a setter?) passing it the newly created class BNRItem if I'm correct? What do "i" and "str" mean though? Are they just variable names?
And all this stuff is set in the header which makes a lot of sense to me, but why do I have to repeat it in the implementation file? It's a bit mixed in my head with javascript functions where you just define it and then call the function anytime you need to refer to it.
Can anyone explain this in plain English please?
Thanks!
1 Answer
Amit Bijlani
Treehouse Guest TeacherAll good questions and critical to understand when you are starting out. I would recommend reading Ben's Beginners Guide to Objective-C or my Introduction to Objective-C.
Answers to some of your questions:
Header and Implementation: The header (.h) defines what the file will do while the implementation file which ends in .m implements them. Think of it as a watch, the face of the watch is your header while the implementation are all the mechanical parts that makes the watch keep time.
Here's an anatomy of one of the methods you mentioned above:
The same method in JavaScript would look like:
function setContainer ( i );

Gary Luce
7,153 PointsGary Luce
7,153 PointsThank you, you're great.