iOS Build a Simple iPhone App with Objective-C Creating a Data Model Creating a Data Collection

Stefan Mach
Stefan Mach
3,691 Points

I think this code challenge is broken.

The code challenge says I am not adding all three items but they are all there, no typos.

Can someone please correct the challenge so it recognizes what is the case. I tested by creating a syntax error and checking preview. It would say I had only one error, so I fixed it, and all it says is I don't have three items listed.

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

    // Add your code below!

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

@end

1 Answer

Martin Wildfeuer
PRO
Martin Wildfeuer
Pro Student 11,071 Points

Hey Stefan Mach,

almost there! You created a property called shoppingList in your interface file:

ViewController.h
@property (nonatomic, strong) NSArray *shoppingList;

We want this property to contain the shopping list objects, like toothpaste etc. In you viewDidLoad code however, you are not initializing this property with the given objects, instead you are allocating and initializing a new NSArray called shopping list:

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

If you wanted to use the existing property, code would be as follows:

ViewController.m
self.shoppingList = [[NSArray alloc] initWithObjects: @"toothpaste", @"bread", @"eggs", nil];

Hope that helps :)

Stefan Mach
Stefan Mach
3,691 Points

Thank you. It did.