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 Build a Simple iPhone App with Objective-C Creating a Data Model Creating a Data Collection

carlos carrillo
carlos carrillo
1,197 Points

not 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];

ViewController.h
#import "UIViewController.h"

@interface ViewController : UIViewController

@property (strong, nonatomic) NSString *shoppingCart;

@property (strong, nonatomic) NSArray *shoppingList;

@end
ViewController.m
#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
Marius Kildedal
8,096 Points

Try putting the self.shoppingCart inside the [ ]

You have to access the index of the array that corresponds to that element.

arrayName[index]

ajor
ajor
8,216 Points

I 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?

Yes, they're the same. One is just a shorthand version of the other called a Literal.

ajor
ajor
8,216 Points

Thank 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]; }

Because 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
ajor
8,216 Points

Ok, 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!

The third item in the array "shoppingList" is the item at index 2. No idea why it's throwing that error. Good luck!

ajor
ajor
8,216 Points

Yes, that's why I am so confused. Well, thank you so much for taking time to answer my questions. I learned quite a bit,