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

Michael Lee
Michael Lee
2,855 Points

Problem with a challenge in the "Creating a Data Model" section of the "Coding a simple app with Objective-C" course

I have a problem with Part 3 of 3 of the challenge, found at this link:

http://teamtreehouse.com/library/build-a-simple-iphone-app-with-objectivec/creating-a-data-model/creating-a-data-collection-2

The challenge is to, "in the viewDidLoad method, retrieve the string eggs from the shoppingList array and store it in shoppingCart."

However, my code (attached), does not seem to work.

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] init];
    self.shoppingList = @[@"toothpaste", @"bread", @"eggs"];
    NSString *shoppingCart = [[NSString alloc] init];
    shoppingCart = [shoppingList objectAtIndex:2];

}

@end

2 Answers

Rashii Henry
Rashii Henry
16,433 Points

hey michael.

It seems to me that you created new instances of shopping cart and Shopping list inside the implementation file && tried adding the egg string to that instance. which isn't working because you made new references instead using what you already have.

In other words, you didn't have to create any new instances of shopping cart inside of your implementation file.

Simply refer to the ones you have in the header file with the self(dot) notation.

You should be good to go bro!