Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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?