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'

shoppingCart = [shoppingList objectAtIndex:2];

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 = @[@"toothpaste", @"bread", @"eggs"];

    shoppingCart = [shoppingList objectAtIndex: 2];
}

@end

1 Answer

Holger Liesegang
Holger Liesegang
50,595 Points

Hi Brendan Corey !

You solved Challenge Task 3 of 3 "We're at the grocery store shopping and we found the eggs. In the header file we have a property, shoppingCart, of type NSString. In the viewDidLoad method, retrieve the string eggs from the shoppingList array and store it in shoppingCart. Hint: use the 'objectAtIndex' method." nearly complete. You only forgot to use "self" while referencing the properties of the class: self.shoppingCart = [self.shoppingList objectAtIndex: 2];

ViewController.h
#import "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 = @[@"toothpaste", @"bread", @"eggs"];
    self.shoppingCart = [self.shoppingList objectAtIndex: 2];
}

@end

Appreciate the assistance. Tried everything. was unaware about using the self.shoppingList objectAtIndex: 2 dot notation within the parentheses.

hamza shaikh
hamza shaikh
5,875 Points

just want to lest you know i also was having trouble with the task 3 but you helped me out thankyou, but i'm a little confuse because in the video when added an array to the label he did not used self.

 (IBAction)showFacts {
    NSArray *facts = [[NSArray alloc]initWithObjects:@"this is a fact",@"this is another fact", nil];
    self.factsLabel.text = [facts objectAtIndex:1];
}

code worked fine even though i did'nt add self. to "[facts objectAtIndex:1]" it will really help me if you can clarify this for me thank you.

Michael Acosta Pegoraro
Michael Acosta Pegoraro
4,911 Points

I was not aware about the self.shoppingList either. Good thing the answer was here and I found out after a while.