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

What is wrong with my code for this code challenge of creating an array of images?

UIImage *image = [UIImage imageNamed:@"robot.png"];
UIImageView *imageView = [[UIImageView alloc] initWithImage:image];

/* Write your code below this line */

NSArray *robotImages = [self.imageView alloc]init];
self.robotImages.animationImages = [[NSArray alloc] initWithObjects:[UIImage imageNamed: @"Robot1.png"],
[UIImage imageNamed: @"Robot2.png"],
[UIImage imageNamed: @"Robot3.png"],
[UIImage imageNamed: @"Robot4.png"], nil];

5 Answers

Amit Bijlani
STAFF
Amit Bijlani
Treehouse Guest Teacher

robotImages should be an instance of NSArray and it's wrong when you specify: [self.imageView alloc]init];.

Your next line correctly creates an instance of NSArray which belongs with NSArray *robotImages =

So it should like like:

NSArray *robotImages =  [[NSArray alloc] initWithObjects:[UIImage imageNamed: @"Robot1.png"],
[UIImage imageNamed: @"Robot2.png"],
[UIImage imageNamed: @"Robot3.png"],
[UIImage imageNamed: @"Robot4.png"], nil];

Secondly, you only use self when you are referring to a property. Here we have declared only instance variables. A property is declared using @property.

The last line of code should use imageView which contains the property animationImages and assign robotImages to it.

thank you! Then shouldn't the next line be this? [self.imageView.animationImages = robotImages]; or self.imageView.animationImages = robotImages;

I still don't really understand when you use the hard brackets.

From the video, we did: self.imageView.animationImages = [[NSArray alloc] initWithObjects: [UIImage imageNamed: @"cball00001"],

So why don't we do the same thing here? I just made the robotImages NSArray so shouldn't this code work?

Amit Bijlani
STAFF
Amit Bijlani
Treehouse Guest Teacher

As I mentioned previously you don't need to reference self as it is used only when accessing a property.

Box brackets are used when calling a method:

Maybe this helps:

// defining a string in javascript
var myString = "Hello World";

// defining a string in objective-c 
NSString *myString = @"Hello World";

// calling a function in javascript on myString
myString.toLowerCase();

// calling a method in Objective-C 
[myString lowercaseString];

then why did we do reference "self" in the video code like so: self.imageView.animationImages = [[NSArray alloc] initWithObjects: [UIImage imageNamed: @"cball00001"], [UIImage imageNamed: @"cball00002"], [UIImage imageNamed: @"cball00003"], [UIImage imageNamed: @"cball00004"], [UIImage imageNamed: @"cball00005"], self.imageView.animationDuration = 1.0;

Is it because in the challenge, we don't have a separate header file, and so we don't have properties? If so, I figured out the next line of code correctly: imageView.animationImages = robotImages; But then, when we have to set the duration, shouldn't this code be correct? imageView.animationDuration = 1.0; or should I use: robotImages.animationDuration = 1.0; (because I already set imageView to robotImages?)

Either way, it's saying both these lines are wrong...

Thanks again

Amit Bijlani
STAFF
Amit Bijlani
Treehouse Guest Teacher

The Code Challenge lives separately from the code we create in the video. The challenge is merely reinforcing what you learned while watching the video. That's why there is no property defined the in the challenge, which is why you cannot use self.

And you are right about this code: imageView.animationDuration = 1.0; Except that the challenge is asking you to set it to 5 seconds.