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

Justin Whedon
Justin Whedon
19,811 Points

Having issues with task 3 of 3 on the shoppingCart challenge.

What am I doing wrong here? Trying to add 'eggs' to the 'shoppingCart'.


import "ViewController.h"

@implementation ViewController

  • (void)viewDidLoad { [super viewDidLoad]; self.shoppingList = [[NSArray alloc] initWithObjects:@"toothpaste",@"bread", @"eggs",nil]; NSString *shoppingCart = [shoppingList objectAtIndex:2]; }

@end

ViewController.h
#import "UIViewController.h"

@interface ViewController : UIViewController

@property (strong, nonatomic) NSString *shoppingCart;
@property (nonatomic, strong) NSArray *shoppingList;

@end
ViewController.m
#import "ViewController.h"

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.shoppingList = [[NSArray alloc] initWithObjects:@"toothpaste",@"bread", @"eggs",nil];
    NSString *shoppingCart = [shoppingList objectAtIndex:2];
}

@end

1 Answer

Rashii Henry
Rashii Henry
16,433 Points

Hey Justin,

once you declare a property in a viewController. It becomes accessible all throughout that class. You did that in your header file flawlessly, with shoppingCart.

However, in your implementation, you created a new instance of shoppingCart, sorta like you didn't want to use what you had already created.

Now the compiler is assigning it to a different location. So try using the syntax you have with your shoppingList. You should be good to go.