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

Assigning an object from a shoppingList NSArray as a shoppingCart NSString value

Can't figure out what's wrong

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!
self.shoppingList = [[NSArray alloc]initWithObjects: @"toothpaste",@"bread",@"eggs",nil];

shoppingCart = [shoppingList objectAtIndex:2];
}

@end

3 Answers

Hi Renan,

You're very close.

You need to use self on the array variable name and the NSString too:

  self.shoppingList = [[NSArray alloc]initWithObjects: @"toothpaste",@"bread",@"eggs",nil];
  self.shoppingCart = [self.shoppingList objectAtIndex: 2];

I hope that helps,

Steve.

Anjali Pasupathy
Anjali Pasupathy
28,883 Points

You're nearly there! You just need to call self.shoppingCart and self.shoppingList in the last line of viewDidLoad:

self.shoppingCart = [self.shoppingList objectAtIndex:2];

I hope this helps!

Sai Kiran Dasika
PLUS
Sai Kiran Dasika
Courses Plus Student 7,278 Points

What you have done is correct except you made a silly mistake by not adding the self keyword before the shopping cart as it's a property of the ViewController class. The following code should work properly

self.shoppingCart = [self.shoppingList objectAtIndex:2];