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

Christian Johansson
Christian Johansson
1,772 Points

I really can't seem to grasp this either

I'm trying to understand how this work but don't seem like i can grasp it at the moment.

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

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

@end
Damien Watson
Damien Watson
27,419 Points

Hi Christian, I started writing a massive response to this, then realised I didn't actually understand what your problem is. Can you be a little more specific? eg:

  • I've created this and it doesn't work
  • Understanding the relationship between the 'h' and 'm' file.
  • Everything

I'm happy to get verbose on this, just want to make sure I'm answering your question. :)

Christian Johansson
Christian Johansson
1,772 Points

Well I wrote self.shoppingCart = [shoppingList objectAtIndex:2]; and I thought that would work. But it seems I don't understand the fundementals.

2 Answers

Damien Watson
Damien Watson
27,419 Points

Ah, you are missing the 'self.' from shoppingList, try:

self.shoppingCart = [self.shoppingList objectAtIndex:2];
Christian Johansson
Christian Johansson
1,772 Points

Now it worked! Hm, don't really know if I should just switch to swift instead since it doesn't seem like I can understand objective C

Damien Watson
Damien Watson
27,419 Points

That is an interesting point, very worth considering. I have been coding in Objective C for around 4 years now. Since Apple is not going to be supporting it and moving ahead with swift...?

I know a company down here that won't employ you if you don't know swift, no matter how many years ObjC.

Christian Johansson
Christian Johansson
1,772 Points

Interesting, but I do wanna learn objective C but maybe it's easier for me to start with swift so I get stuff going. I think I learn best by making stuff and having fun!

Damien Watson
Damien Watson
27,419 Points

Having something to build does make it a lot easier. With ObjC, just remember that any variable declared in your header file '.h' needs to be accessed in the implementation file '.m' by putting 'self.' at the front and you should be alright. :)