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 trialerickwatson
1,783 PointsNo idea how to do this.
Let's add a few items section (task 2) for the Creating a Data Collection challenge. It is asking me to use 'self' and dot notation to initialize an array of three items to shoppingList.
Usage of 'self' in lesson much more limited. I don't remember much instruction on how much we treat self (and dot syntax) in the Objective-C Basics, and certainly not whether many of the same operations as are used in bracket syntax can just be dropped in. Even Googling 'self in Objective-C' or using the documentation is not giving me the answer.
What am I missing? I have tried including NSArray in brackets, not including it, various dot-suffixes after self.shoppingList, initWitharray, initWithObject, etc.
#import "UIViewController.h"
@interface ViewController : UIViewController
@property (strong, nonatomic) NSArray *shoppingList;
@property (strong, nonatomic) NSString *shoppingCart;
@end
#import "ViewController.h"
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Add your code below!
self.shoppingList = [shoppingList initWithArray: @"toothpaste", @"bread", @"eggs"];
}
@end
2 Answers
Chris Shaw
26,676 PointsHi Erick,
To create a new NSArray
instance we need to create it via alloc
which creates a new instance in memory, then we need to use the initWithObjects
method to add 3 objects to it, the other thing to note is you always need nil
as the last value as it defines the end of the array in memory.
self.shoppingList = [[NSArray alloc] initWithObjects:@"toothpaste", @"bread", @"eggs", nil];
Happy coding!
erickwatson
1,783 PointsOh crap. I remember using that exact syntax in one of my attempts, but in none of them did I use 'nil'. Ugh.
Thank you very much for help!
erickwatson
1,783 Pointserickwatson
1,783 PointsNote the code pasted in is just ONE of many many attempts...