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

Sean Craig
Sean Craig
5,178 Points

Im struggling with how to initialize the array for the grocery shopping list.

I cant figure out what I'm doing wrong trying to initialize the array.

ViewController.h
#import "UIViewController.h"

@interface ViewController : UIViewController

@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] initWithObjects:@"toothpaste", @"bread", @"eggs", nil];

}

@end

4 Answers

Stepan Ulyanin
Stepan Ulyanin
11,318 Points

Hi! First of all you already have the shoppingList property declared in the interface of the controller, so in your implementation it would be something like:

- (void)viewDidLoad {
    [super viewDidLoad];

    // Add your code below!

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

Other than that, the array initialization seems fine!

Stepan Ulyanin
Stepan Ulyanin
11,318 Points

According to the apple reference:

Although it’s best practice for an object to access its own properties using accessor methods or dot syntax, it’s possible to access the instance variable directly from any of the instance methods in a class implementation. The underscore prefix makes it clear that you’re accessing an instance variable rather than, for example, a local variable.

When you use the _variable, it is the instance variable and it is not recommended to access it directly unless you have the real reason to

Mahesh Babu
Mahesh Babu
3,402 Points

Hi,

You have declared a property of type shoppingList, so all you have to do is use _ shoppingList variable, but i would definitely recommend self.shoppingList as that is the standard syntax unless you are using an initializer , you should not use the _shoppingList.

I would definitely let the standard syntax go off the box and do the literal syntax nice and clean,

self.shoppingList = @[@"toothpaste", @"breads",@"eggs"];

Caleb Kleveter
MOD
Caleb Kleveter
Treehouse Moderator 37,862 Points

Try this:

_shoppingList = [[NSArray alloc] initWithObjects:@"toothpaste", @"bread", @"eggs", nil];
Stepan Ulyanin
Stepan Ulyanin
11,318 Points

Why would you mess up with the instance variable in this case? I mean isn't it a bad practice to mess around with the instance variable unless you are writing the initialization method?