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

initWithObjects problem

I can't complete the task. It requires me to add 'toothpaste' 'bread' and 'eggs' into the array shoppingList. I dont know where the problem is. It looks okay.


#import "UIViewController.h"

@interface ViewController : UIViewController

@property (strong, nonatomic) NSString *shoppingCart;
@property (strong, nonatomic) NSArray *shoppingList;

#import "ViewController.h"

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    // Add your code below!
  [self.shoppingList initWithObjects:@"toothpaste",@"bread",@"eggs",nil];


1 Answer

Iris Avalon
Iris Avalon
14,477 Points

Looks like there is a bit of an error over in your ViewController.m file. Down where you have

[self.shoppingList initWithObjects:@"toothpaste",@"bread",@"eggs",nil];

In order to init your shopping list, you need to specifically init it to a NSArray object. Like this!

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

Your header file knows the shopping list is going to eventually have an NSArray object in it, but in your implementation you'll need instantiate a new array to that object. I hope that doesn't sound too confusing! The header file creates the shoppingList variable, and in your implementation you need to assign an NSArray to it :)

Great explanation man!