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 trialJustin Greenough
2,865 PointsCan'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?
Stone Preston
42,016 PointsAaron Ackerman he does have a mutable dictionary named albumMutable.
agreatdaytocode
24,757 Pointsyeah just noticed your post lol.
4 Answers
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.
Justin Greenough
2,865 PointsYou are right! Thank you!
Stone Preston
42,016 Pointsno problem
Justin Greenough
2,865 PointsIncidentally, 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
42,016 Pointsthat 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.
Justin Greenough
2,865 PointsYes 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?
agreatdaytocode
24,757 PointsWhat challenge is this?
Justin Greenough
2,865 PointsIt's the last set of objectives in the 'dive deep' Objective-C Basics: Foundation Framework.
agreatdaytocode
24,757 PointsNSDictionary *album = @{@"title":@"Abbey Road", @"artist":@"The Beatles"};
NSMutableDictionary *albumMutable = [NSMutableDictionary dictionaryWithDictionary:album];
Thats what worked for me. Did you get the same thing?
Justin Greenough
2,865 PointsThat's what worked for me also, as you can see from my comments above.
agreatdaytocode
24,757 Pointsagreatdaytocode
24,757 PointsYou have NSDictionary, you need a NSMutableDictionary also make sure you name it albumMutable :)