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

iOS Development > Data Modeling > Using A Custom Class Quiz

im having trouble with the quiz questions on Using a Custom Class.

1) Create an instance of the Song with alloc and init Song *popSong = ______________;

2) Create an instance of the Song class with the convenience constructor songWithSinger:(NSString *)s and singer named "James Brown". Song *popSong = ____________;

3) Create an instance of the Song class with the designated initializer initWithSinger:(NSString *)s and singer named "James Brown". Song *popSong = ______________;

I'm not looking directly for the answers. just a little assistance. maybe someone could help me approach these questions differently. i understand all of the vocabulary, i just need for someone to explain when it says "create an instance" what is it basically asking for.. when am I suppose to know what to write specifically. I believe i just need the question broken down for me so i can fully understand why the question is being asked the way it is..

how am i suppose to know when my whole line of code is wrong versus a punctuation mark?

2 Answers

Amit Bijlani
STAFF
Amit Bijlani
Treehouse Guest Teacher

Creating an instance is when you declare a variable from a class. When you call the alloc method on a class it allocates space memory in memory which returns an instance of that particular class. Subsequently calling the init method initializes the newly created instance with default values.

so is declaring a variable from a class the same thing as calling a variable name that you created..?

like NSString *myString = @"treehouse";

would creating an instance be declaring the variable name "myString" somewhere later in my code?

Amit Bijlani
Amit Bijlani
Treehouse Guest Teacher

That's right myString is an instance of NSString. The statement above is the same as saying NSString *myString = [[NSString alloc] initWithString:@treehouse"]; except that the compiler knows that anything prefixed with an at symbol (@) is a literal which means it's a shorthand way of declaring an instance. We are working on an Objective-C course but meanwhile you might benefit from a getting a book on Objective-C.

i have a book and a few great resources already. Im really just trying to juggle college and treehouse. On the other hand, i think something that would help a lot of us would be completing more simple projects on our own. BUT using the information that you guys just taught us in that specific learning adventure.

Alot of us find it hard to retain alot of the information.

I believe it would help if you guys show us that after you teach us one thing, those same methods and techniques that you just taught can apply to other projects. Then leave it up to us to stretch our creativity and completing the projects.

just a suggestion though.

if I'm asked to create an instance of UIImage called 'image' given the name of the image is 'placeholder.png'.

would this be correct? if not, could you tell me which side of my equation needs work?

UITableViewCell *cell = [[UITableViewCell alloc] init];
/* Write your code below this line */
cell.imageView.image = [UIImage imageNamed:@"placeholder.png"];
Amit Bijlani
Amit Bijlani
Treehouse Guest Teacher

Your answer is right! You got it.

EDIT: If the question is "Create an instance of UIImage called 'image' given the name of the image is 'placeholder.png'."

Then on the left you need to create an object called image:

UIImage *image = [UIImage imageNamed:@"placeholder.png"];

thank you,

now let me ask you this, Do instances always have to be implied by an '-' before your line of code?

another question i have about instances is in what cases do you have to call an instance versus using a method.

or am i confused about them both and that an instance usually consists of methods.

basically my second question is.. Is an instance a series of methods followed by a "-" sign?

Amit Bijlani
Amit Bijlani
Treehouse Guest Teacher

Answers to your questions:

Q: Do instances always have to be implied by an '-' before your line of code? No instance methods start with a '-' and not an instance variable.

Let's take the class UIView as an example. To create an instance of UIView called myView you would do the following:

UIView *myView = [[UIView alloc] init];

In the above example the variable myView is an instance of the class UIView. Think of the the class as a template that defines the properties and methods. Now you create your occurance (instance) of a view using this template (class) called UIView.

Q: In what cases do you have to call an instance versus using a method.

A method is something you call upon an instance. So taking the above examples of myView. Now that you have an instance of UIView you can call an instance method:

          Method
              |
[myView sizeToFit];
    |
Instance
Mike Mitchell
Mike Mitchell
Courses Plus Student 27,026 Points

Rashii and Amit,

Thanks for posting this discussion. I'm struggling with the same quiz and can't seem to figure out where I've lost the track.

I've watched the Using a Custom Class video more times than I can count. I think I need some reading material. Do either of you have any recommendations?

I found an interesting section in the Objective-C documentation called Defining Classes. Going to start there. Any other suggestions would be great!

Best,

Mike

One suggestion would be to look forward to the Objective-C lessons that come out sometime this month.

Another would be to look for some Objective-C primers. I don't have the links to any of the threads anymore but they're somewhere in these forums. i'll go look for them and send you the links.