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 trialSayana Narayanan
1,221 Pointsarrays
NSArray *booksArray = [NSArray arrayWithObjects : @"Hamlet",@"King Lear",@"Othello",@"Macbeth",nil];
have this array now second questin is : Create a string variable called 'bookTitle' and assign it the third item from the 'booksArray'.
my answer was
NSString *bookTitle = [self.booksArray objectAtIndex:2];
but it showing error can you help me to fix it.
3 Answers
Kevin Lozandier
Courses Plus Student 53,747 PointsHi, Sayana Narayanan:
You do not need to associate self
with booksArray because of the scoping context of this challenge.
Correcting that mistake, you should have the following code typed:
NSString *bookTitle = [booksArray objectAtIndex:2];
Pro tip
Even better, though it might not be recognized by the challenge for this problem, you can access the index you want of an array using square brackets like most languages.
You can also simplify NSArray
instantiations with the literal syntax:
NSArray *booksArray = @[@"Hamlet",@"King Lear",@"Othello",@"Macbeth"];
NSString *bookTitle = booksArray[2];
Dino Paškvan
Courses Plus Student 44,108 PointsbooksArray
in this situation is not a class property, so you don't need the self
part to access it.
AbdelGhafour Mohie
1,876 PointsTry removing "self".
Sayana Narayanan
1,221 PointsSayana Narayanan
1,221 PointsThanks