Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

iOS

Can't pass challenge: What's wrong with my code?

The challenge:

"Declare an NSMutableDictionary variable named 'albumMutable' that is initalized using the previously created dictionary named 'album'."

My answer:

NSDictionary *album = @{@"title":@"Abbey Road", @"artist":@"The Beatles"};
NSMutableDictionary *albumMutable = [[NSMutableDictionary alloc] dictionaryWithDictionary:album];

The error:

"Bummer! Make sure you are creating an NSMutableDictionary with the correct variable name, and check your syntax!"

What is the problem?

You have NSDictionary, you need a NSMutableDictionary also make sure you name it albumMutable :)

Stone Preston
Stone Preston
42,016 Points

Aaron Ackerman he does have a mutable dictionary named albumMutable.

yeah just noticed your post lol.

4 Answers

Stone Preston
Stone Preston
42,016 Points
[[NSMutableDictionary alloc] dictionaryWithDictionary:album]

there is your issue.

i think the convenience constructor is

 [NSMutableDictionary dictionaryWithDictionary:whateverDictionary]

you dont have to allocate it, the convenience constructor does that for you.

You are right! Thank you!

Incidentally, I first had written

NSMutableDictionary *albumMutable = [[NSMutableDictionary alloc] initWithDictionary:album];

which also didn't pass though I believe it is correct. I only altered it to use dictionaryWithDictionary in response to the hint. Do you know why my original submission failed?

Stone Preston
Stone Preston
42,016 Points

that looks correct. Sometimes the code engine is looking for a certain method. In this case its probably wanting you to use the convenience constructor dictionaryWithDictionary and not the initWithDictionary method. But what you had first looks correct to me.

Yes I also thought it was correct. In fact nowhere in the preceding video tutorials is the convenience constructor nor dictionaryWithDictionary mentioned. Is a reference to this method contained in some documentation provided with these lessons somewhere I don't know about?

What challenge is this?

It's the last set of objectives in the 'dive deep' Objective-C Basics: Foundation Framework.

NSDictionary *album = @{@"title":@"Abbey Road", @"artist":@"The Beatles"};
NSMutableDictionary *albumMutable = [NSMutableDictionary dictionaryWithDictionary:album]; 

Thats what worked for me. Did you get the same thing?

That's what worked for me also, as you can see from my comments above.