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

Romi Stepovich
Romi Stepovich
8,473 Points

Here is the message I am getting: Remember to use the keyword 'self' to access the 'shoppingList' property and initializ

I have tried the code a few different ways. This was my latest try...

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

    // Add your code below!

   NSArray *shoppingList = [[NSArray alloc]initWithObjects:@"toothpaste", @"bread", @"eggs", nil];

   self.shoppingList.text = [shoppingList objectAtIndex:3];




}
@end

2 Answers

Hayden Pennington
Hayden Pennington
6,154 Points

Your ViewController.h header file is good. The problem is in your implementation file.

To initialize shoppingList, you should remove 'NSArray' from the statement, because you have already declared it in the header file. You also need to add self, to specify that you are referring to "this", instance of the class.

// So initialize shoppingList like this
self.shoppingList = [[NSArray alloc] initWithObjects: @"toothpaste", @"bread", @"eggs", nil];

Then it asks you to put the item 'eggs', which is a NSString, from the shoppingList (a NSArray of NSString), into the shoppingCart.

// So assign index 2 (because arrays start at index 0) to shoppingCart
self.shoppingCart = [self.shoppingList objectAtIndex: 2];
Romi Stepovich
Romi Stepovich
8,473 Points

Thank you for the clarifications! That all makes sense and I completely forgot to start from 0. I appreciate the help.