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

iOS

2 Obj C questions

I'm almost done with the simple iphone app project and I'm trying to understand two things that don't seem to be covered in the videos.

First, how come when we create a custom class, we write in the implementation file - (NSArray *) name {} ? Why is the * enclosed in the parenthesis with NSArray? I understand that if it was *name I'd be declaring an instance variable, but I don't understand this.

Second, is a convenience constructor the same as a method? Thanks for the help.

2 Answers

- (NSArray *) name {

} 

that is a method defintion, most likely for a getter method that returns an NSArray object (remember all objects in objective c are pointers) thats why its (NSArray *) instead of just (NSArray)

a convenience constructor is special kind of method. It allocates and initializes an object in one method call (so you dont have to do [Class alloc] init]).

Thank you very much! ok - so if I understand you correctly, it's defining another type of return like - (BOOL) or - (void). As for the conveninece constructor though, how do I decide whether to use one or just a regular method? Based on experience or is there a general rule?

you use them whenever you want, there is no "when should I use it over the regular way" . most of the foundation classes (NSArray etc) have several convenience constructors (a class can have more than one) such as [NSArray arrayWithObjects:@"foo", @"bar", nil]; that you can use, but you can just use alloc and initWithObjects as well. Its just preference and whether the convenience constructor exists. You can make your own convenience constructors for custom classes as well. I dont really use them that much though, but I havent really been coding that long either.

That's very clear. Thanks!