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

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!