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 Object-Oriented Objective-C Memory, Arrays and Loops, Oh My! Alloc and Init

Christian Antfeld
Christian Antfeld
1,087 Points

alloc and init challenge compiler error

In the alloc and init challenge in the Objective-C track I am receiving a compiler error and I'm not sure why.

variable_assignment.mm
NSMutableDictionary *carDict =[carDict initWithObjectsAndKeys : @"Honda", "Make", @"Accord", "Model", nil];

The goal is to create a dictionary that has those keys and values in it and to allocate memory for it as well. I do not remember seeing how to allocate that memory while creating all of this on a single line (one of the requirements of the challenge).

1 Answer

Arman Arutyunov
Arman Arutyunov
21,900 Points

You've missed the syntax. In objective c to allocate and initialize it you have to firstly name the class you are working with (NSMutableDictionary), then call its alloc method and then call its initWithObjectsAndKeys method that you actually did call (but without calling NSMutableDictionary class and allocating it it doesn't work).

The actual working piece of code:

NSMutableDictionary *carDict = [[NSMutableDictionary alloc]initWithObjectsAndKeys:@"Honda", @"Make",@"Accord",@"Model", nil];