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
PRO
Sai Kiran Dasika
Pro 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];