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

Not understanding what I am doing wrong with question 3 of 3 in 'Creating a Data Collection'

Ive tried a couple of methods, but now I feel I'm just stumbling around in the dark.

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];

    // Add your code below!

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

@end

2 Answers

Hi James,

You are almost there at completing your challenge. There is a minor mistake with the method 'objectsAtIndex' which should be replaced by 'objectAtIndex' leading to the correct method call.

self.shoppingCart = [self.shoppingList objectAtIndex:2];

Hope this helps :)

Thank you so much! That worked... though its frustrating that it was something so simple, after spending so much time on it. Live & learn.

I am glad you figured it out :)

Caleb Kleveter
MOD
Caleb Kleveter
Treehouse Moderator 37,862 Points

You might need to use an underscore vs. self:

self.shoppingCart = [_shoppingList objectAtIndex:2];

Well you can use the instance variable (ivar) to call a method as mentioned above but I recommend not to use it for that purpose because ivars are basically used for initializing a property under the designated initializer rather than calling class methods. I guess I am going the right way?