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

No idea how to do this.

Let's add a few items section (task 2) for the Creating a Data Collection challenge. It is asking me to use 'self' and dot notation to initialize an array of three items to shoppingList.

Usage of 'self' in lesson much more limited. I don't remember much instruction on how much we treat self (and dot syntax) in the Objective-C Basics, and certainly not whether many of the same operations as are used in bracket syntax can just be dropped in. Even Googling 'self in Objective-C' or using the documentation is not giving me the answer.

What am I missing? I have tried including NSArray in brackets, not including it, various dot-suffixes after self.shoppingList, initWitharray, initWithObject, etc.

ViewController.h
#import "UIViewController.h"

@interface ViewController : UIViewController

@property (strong, nonatomic) NSArray *shoppingList;

@property (strong, nonatomic) NSString *shoppingCart;

@end
ViewController.m
#import "ViewController.h"

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    // Add your code below!
    self.shoppingList = [shoppingList initWithArray: @"toothpaste", @"bread", @"eggs"];
}

@end

Note the code pasted in is just ONE of many many attempts...

2 Answers

Chris Shaw
Chris Shaw
26,676 Points

Hi Erick,

To create a new NSArray instance we need to create it via alloc which creates a new instance in memory, then we need to use the initWithObjects method to add 3 objects to it, the other thing to note is you always need nil as the last value as it defines the end of the array in memory.

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

Happy coding!

Oh crap. I remember using that exact syntax in one of my attempts, but in none of them did I use 'nil'. Ugh.

Thank you very much for help!