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
Joshua Cawthorne
1,742 PointsBuild a Simple iPhone App w/ Objective-C -- Data Collection Code challenge Task #3
Having trouble with this skills test that I feel should be working. I’ve tried several ways to do this however; the error generator is saying I should be using _shoppingCart and _shoppingList. It is my understanding that the _someVariable is used only when variables have been synthesized.
The tutorial hasn’t covered this yet, neither does it accept the following answers I tried providing. I believe I did it exactly the way the tutorial just covered and below are just two of the many attempts, including initializing the string variable.
Thanks,
CURRENT code:
Bummer! Remember array indexes start at zero. Access the third item in the array and assign it to a string named `shoppingCart`.
#import "ViewController.h"
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.shoppingList = [[NSArray alloc] initWithObjects: @"toothpaste", @"bread", @"eggs", nil];
self.shoppingCart.text = [shoppingList objectAtIndex: 2];
}
@end
//WITH _variable
#import "ViewController.h"
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
_shoppingList = [[NSArray alloc] initWithObjects: @"toothpaste", @"bread", @"eggs", nil];
_shoppingCart.text = [_shoppingList objectAtIndex: 2];
}
@end
//WITHOUT _variable
#import "ViewController.h"
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
self.shoppingList = [[NSArray alloc] initWithObjects: @"toothpaste", @"bread", @"eggs", nil];
self.shoppingCart.text = [shoppingList objectAtIndex: 2];
}
@end
2 Answers
Radoslav Radev
4,732 PointsViewController.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 = [_shoppingList objectAtIndex:2];
}
@end
Caleb Kleveter
Treehouse Moderator 37,862 PointsTake a look at this thread, it might help you find the answer.
Joshua Cawthorne
1,742 PointsJoshua Cawthorne
1,742 PointsAs a note, I also tried [self.shoppingList objectAtIndex: 2]; also. I also was incorrect that the synthesizing I believe is for backwards compatibility of older iOS versions. The _ is for the actual memory location of the variable if Im saying this right.