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

General Discussion

Quick Objective c literal question

If I write just @"string"; , will this create an object and allocate memory to it? Or do I need to do like NSString *string = @"string";

2 Answers

you will have to use

NSString *string = @"string";

just typing @"string; would either not compile or maybe just give you a warning that its not being used for anything

technically using @"string" DOES create the object and allocate memory for it, thats the whole point of literals, to make it easier to allocate and init commonly used foundation objects without having to use something like

[[NSArray alloc] initWithObjects:@"one", @"two", @"three", nil];

you can just use a literal instead. But without assigning your NSString object to the literal you have no way to reference it.

Thanks a lot =)