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 trialEric Griffin
2,553 PointsHi, 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...
#import "UIViewController.h"
@interface ViewController : UIViewController
@property (strong, nonatomic) NSString *shoppingCart;
@property (strong, nonatomic) NSArray *shoppingList;
@end
#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
3 Answers
Kieran Tross
8,266 PointsCan 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
9,258 PointsWhy are using " self.shoppingList.text " ? This has got nothing to do with a text property.
Patrick Cooney
12,216 PointsGood 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
12,216 PointsYou're not in the viewDidLoad
method. If you look closely you put your code outside the terminating brace.
Oluwayanmife Akintola
9,258 PointsOluwayanmife Akintola
9,258 PointsI 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];