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

Wayne Knoesen
Wayne Knoesen
9,435 Points

NSDictionary

At work I was shown a nicer way to make a dictionary (IMO),

NSDictionary *dict = @{@"title" : @"My title", @"Another key" : @"The object"};

It I can get it in the log so it seems like I dont need to alloc or anything either. I was wondering if this is good practice? It was easier to understand for me and seems easier to write. The guy who showed me is a good programmer but doesnt care much for teaching ;-)

Wesley Ellis
Wesley Ellis
3,818 Points

Reviewing the Apple documentation file on NSDictionary, I find "If you don’t want a temporary object, you can also create an empty dictionary using alloc... and init.".

So, while you may use the method you mention above, you must keep in mind that <code>dict</code> will be in the autoReleasePool. If you need to use it elsewhere, it would be best to allocate and initiate an instance.

1 Answer

Amit Bijlani
STAFF
Amit Bijlani
Treehouse Guest Teacher

This is called a literal and is a perfectly legitimate way to write code in Objective-C. The latest version of the compiler introduced a feature called Objective-C literals. You can read more about it here: http://clang.llvm.org/docs/ObjectiveCLiterals.html