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 trialAleks Dahlberg
19,103 PointsWhat have I missed?
Task: Create an array named 'booksArray' with the following book titles: 'Hamlet', 'King Lear', 'Othello', 'Macbeth'.
my code:
self.booksArray = [NSArray arrayWithObjects:@"Hamlet", @"King Lear", @"Othello", @"Macbeth", nil];
7 Answers
Bradley White
21,285 PointsThe code challenge engine isn't importing a .h file so your self.booksArray doesn't have anything to reference back to. In Xcode, if you have a property declared in the .h file as:
@property (nonatomic, strong) NSArray *booksArray;
then the code you have typed in would work. :)
Here is a shorthand version of that will work with the code challenge.
NSArray *booksArray = @[@"Hamlet", @"King Lear", @"Othello", @"Macbeth"];
Here is the long version:
NSArray *booksArray = [NSArray arrayWithObjects:@"Hamlet", @"King Lear", @"Othello", @"Macbeth", nil];
The alloc and init methods are replaced with @[ ] with the objects inside the brackets and nil is no longer needed. It works the same.
It will work for NSArray in Xcode but not for a NSMutableArray.
If you are in Xcode and the .h file is imported then you could set it up like this:
self.booksArray = @[@"Hamlet", @"King Lear", @"Othello", @"Macbeth"];
Aleks Dahlberg
19,103 PointsThanks that worked!
Aleks Dahlberg
19,103 PointsQ= Create a string variable called 'bookTitle' and assign it the third item from the 'booksArray'.
My code:
NSArray *booksArray = [[NSArray arrayWithObjects] alloc:@"Hamlet", @"King Lear", @"Othello", @"Macbeth", nil]; NSString *bookTitle = [NSArray objectAtIndex:@"Othello"];
Having trouble with this too. Sorry for the boring questions :P
Bradley White
21,285 Pointsthe objectAtIndex method requires a number, not a string. (Hint: it's not a 3. Remember how computers count, not humans.)
Aleks Dahlberg
19,103 PointsI had tried "2" before but had no greenlight
Aleks Dahlberg
19,103 PointsNSString *bookTitle = [booksArray objectsAtIndex:2];
NSString *bookTitle = [booksArray objectsAtIndex:@"2"];
Neither worked
Bradley White
21,285 PointsYou typed objectsAtIndex.
Use objectAtIndex