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

Laura Kirby
Laura Kirby
1,814 Points

Initialize NSArray with three NSString objects. My code works in Xcode but is not evaluated as a correct solution here.

I have tested 3 different ways to initialize the NSArray with the 3 NSString objects. All three of my implementations work in Xcode but are not considered a "solution" to the quiz question.

I also tried:

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

and

NSArray *shoppingList = @[@"toothpaste", @"bread", @"eggs"];

What am I missing?

ViewController.h
#import "UIViewController.h"

@interface ViewController : UIViewController
@property(nonatomic, strong)NSArray *shoppingList; 
@property (strong, nonatomic) NSString *shoppingCart;

@end```  

ViewController.m
```#import "ViewController.h"

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    // Add your code below!
    NSArray *shoppingList = [NSArray arrayWithObjects:@"toothpaste", @"bread", @"eggs", nil];
}

@end```

2 Answers

Jeremy Faith
PLUS
Jeremy Faith
Courses Plus Student 56,696 Points

You declared NSArray *shoppingList in the header file. In the implementation file you can access @property NSArray *shoppingList using the keyword self.

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

Hope this helps.

Laura Kirby
Laura Kirby
1,814 Points

I see, rather than accessing a property on on the view controller, I was creating a completely new NSArray and accessing items from that array. Thank you Jeremy!