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

Adam Berkowitz
Adam Berkowitz
2,673 Points

Blog Reader Question re Designated Initializers/Convenience Constructors/Custom Class

So I just finished watching the "Creating a Custom Class" video for the blog reader project and I'm confused by part of it. In the previous video we made the designated initializer and convenience constructor for title. Then, in the Custom Class video, Amit uses those to write

'''objectiveC for (NSDictionary *bpDictionary in blogPostsArray) { BlogPost *blogPost = [BlogPost blogPostWithTitle:[bpDictionary objectForKey:@"title"]]; blogPost.author = [bpDictionary objectForKey:@"author"]; [self.blogPosts addObject:blogPost]; } ''' How come we didn't create a designated initializer and convenience constructor for "author" as well as "title"? It seems strange to me to use two different ways to accomplish this. I understand that you can't really write

'''objectiveC BlogPost *blogPost = [BlogPost blogPostWithTitle:[bpDictionary objectForKey:@"title"]]; BlogPost *blogPost = [BlogPost blogPostWithAuthor:[bpDictionary objectForKey:@"author"]]; '''

because the instance variable *blogPost can't be used twice in this way. But is there a way to combine them into one line? What am I missing here? Thanks.

Adam Berkowitz
Adam Berkowitz
2,673 Points

Also - I can't figure out how to properly format this code using the ''' marks. If someone could help me out I'd appreciate it.

Stone Preston
Stone Preston
42,016 Points

the marks are back ticks ` not apostrophes '. the back tick on my keyboard is next to the 1 key. its the same key as the ~

Adam Berkowitz
Adam Berkowitz
2,673 Points

thank you! I was driving myself crazy trying to figure that out.

3 Answers

Stone Preston
Stone Preston
42,016 Points

you could easily create a convenience constructor blogPostWithTitle:(NSString *)title andAuthor:(NSString *)author if you wanted to. I think amit was just using the different ways to show you the multiple ways of doing it.

Adam Berkowitz
Adam Berkowitz
2,673 Points

I see. I tried creating multiple convenience constructors, but what you're showing me seems to be a longer one. This is like how some methods have multiple places to put in information right?

Stone Preston
Stone Preston
42,016 Points

yes. it has multiple arguments. Methods can have more than one argument. So you pass in two strings to it, the title and the author, and it uses them somewhere in the body of the method.

Adam Berkowitz
Adam Berkowitz
2,673 Points

Ok. I think I'm beginning to understand more. My programming background is in Max/MSP which is for data and audio work. Usually we would create an object like [cycle~] (which is for waveforms) and then pass it arguments for frequency and amplitude. Same kind of idea here.