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 trialcarlos carrillo
1,197 Pointsnot sure how I'm meant to retrieve the string eggs from the shoppingList array and store it in shoppingCart.. :(
this is what I've typed in.
self.shoppingCart = [ShoppingList objectAtIndex:2];
#import "UIViewController.h"
@interface ViewController : UIViewController
@property (strong, nonatomic) NSString *shoppingCart;
@property (strong, nonatomic) NSArray *shoppingList;
@end
#import "ViewController.h"
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.shoppingList = [[NSArray alloc] initWithObjects:@"toothpaste", @"bread", @"eggs", nil];
self.shoppingCart = [shoppingList objectAtIndex:2];
}
@end
7 Answers
Marius Kildedal
8,096 PointsTry putting the self.shoppingCart inside the [ ]
Stephen Whitfield
16,771 PointsYou have to access the index of the array that corresponds to that element.
arrayName[index]
ajor
8,216 PointsI am afraid I need a little bit more help. Why do we have to access the index of the array via arrayName[index]? Isn't [arrayName objectAtIndex:index] doing exactly that?
Stephen Whitfield
16,771 PointsYes, they're the same. One is just a shorthand version of the other called a Literal.
ajor
8,216 PointsThank you for your quick reply, Stephen.
I have to admit now I am profoundly confused. Why is the following code not working hten?
-
(void)viewDidLoad { [super viewDidLoad];
// Add your code below! self.shoppingList = [[NSArray alloc] initWithObjects:@"toothpaste", @"bread", @"eggs", nil];
shoppingCart = [shoppingList objectAtIndex:2]; }
Stephen Whitfield
16,771 PointsBecause you aren't setting your properties correctly. If you have a property called shoppingCart and a property called shoppingList, if you want to access those properties, you have to either prepend "self." (or an underscore, but we'll use self. here).
self.shoppingList = @[@"toothpaste", @"bread", @"eggs"];
self.shoppingCart = shoppingList[2];
ajor
8,216 PointsOk, that makes sense. However, this still returns the following error:
Bummer! Remember array indexes start at zero. Access the third item in the array and assign it to a string named shoppingCart
.
Thank you so much for your help. It is much appreciated!
Stephen Whitfield
16,771 PointsThe third item in the array "shoppingList" is the item at index 2. No idea why it's throwing that error. Good luck!
ajor
8,216 PointsYes, that's why I am so confused. Well, thank you so much for taking time to answer my questions. I learned quite a bit,