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 clue what im doing wrong plz help

says remember to use self

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!
    self.shoppingList.text = [shoppingList objectsAtIndex:@"toothpaste", @"bread", @"eggs"];
}

@end

1 Answer

Martin Wildfeuer
PLUS
Martin Wildfeuer
Courses Plus Student 11,071 Points

Hey there!

self.shoppingList.text = [shoppingList objectsAtIndex:@"toothpaste", @"bread", @"eggs"]

An NSArray has no property or method called text. XCode will warn you right away. You have declared shoppingList in your header file, so lets use it directly, withouttext`

self.shoppingList = // [shoppingList objectsAtIndex:@"toothpaste", @"bread", @"eggs"];

But before we can use an NSArray we always have to initialize it first! The method you are using here is not for initializing an array, but for retrieving items from an already initialized array (though the syntax is not correct, but that would be too much for now...).

The initializer you are looking for is initWithObjects You can find this method and all other initializers in the Apple Developer Docs.

Let's give it a try:

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

The initWithObjects initializer needs a nil as the last value, so it knows when you are finished adding your items. As mentioned before, the docs show you more options to initialize an array, so flick through it a bit.

Hope that helps!