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

Hi, I am stuck on a Creating a Data Collection

Hi, I am stuck on this question. Any help would be greatly appreciated. Thanks!

Let's add a few items to our grocery list. Switch to the implementation file and in the viewDidLoad method, initialize the shoppingList array to the following items: 'toothpaste', 'bread' and 'eggs'.

This is what I thought would have worked...

ViewController.h
#import "UIViewController.h"

@interface ViewController : UIViewController

@property (strong, nonatomic) NSString *shoppingCart;

@property (strong, nonatomic) NSArray *shoppingList;

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

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
}


      NSArray *list = [[NSArray alloc] initWithObjects: @"toothpaste", @"bread", @"eggs", nil];
      self.shoppingList.text = [list objectAtIndex: 1];



@end
Oluwayanmife Akintola
Oluwayanmife Akintola
9,258 Points

I was stuck as well. I just googled stuffs and I looked at the editor tab which gives you the details of the errors in your code. The code below will work:

@property (strong, nonatomic) NSArray *shoppingList;

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

_shoppingCart = [self.shoppingList objectAtIndex:2];

3 Answers

Kieran Tross
Kieran Tross
8,266 Points

Can anyone tell me what's wrong with my code.

@implementation ViewController

  • (void)viewDidLoad { [super viewDidLoad];

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

self.shoppingList.text = [list objectAtIndex: 1]; }

@end

Oluwayanmife Akintola
Oluwayanmife Akintola
9,258 Points

Why are using " self.shoppingList.text " ? This has got nothing to do with a text property.

Patrick Cooney
Patrick Cooney
12,216 Points

Good catch! I didn't even see this. I just saw the closing brace before all the code and didn't bother to read the rest of it.

Patrick Cooney
Patrick Cooney
12,216 Points

You're not in the viewDidLoad method. If you look closely you put your code outside the terminating brace.