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 Build a Blog Reader iPhone App Data Modeling Designated Initializers and Convenience Constructors

Mike Lange
Mike Lange
794 Points

Why can you set "author" variable under "bp", when in "bp" we have only called the "title" property?

Not really understanding why when we define the "bp" variable that calls the title property that we can define also the author variable, when in blogposts under the title method we have no definition or call to the "author" property....

eg.

BlogPost *blogPost = [[BlogPost alloc] initWithTitle:@"Dummy Title"];
blogPost.author = @"hello";
  1. blogPost allocated and initialised title method.
  2. blogPost can now see and set "author" property - how?

Is someone able to explain this?

Is this simply a case that if you initialise any variable under the class (ie. "*blogPost" above) that this variable can then also set and define any other property defined within the same class file (ie. "BlogPost" class) via "{variable}.{propertytitle}" eg. "blogPost.author".

1 Answer

Initializing something means setting a initial value. Designated initializer means these value( in this case - author) must be when initializing it. You can also pass a another augment for the title if you want, so when you initialize it, you will need both the title and the author. In our case, it's optional to add a author.

The reason why you can do all this is because, at the header file you added the line - (blogPost ) initWithTitle: NSString title; -(id) is the return type, which consists of 2 instance variable. The compiler knows there are 2 instance variable, because it's in the same class.