Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should I release an NSDictionary instantiated through the form @{}?

I have the following code:

NSDictionary *dict = @{ @"myKey" : @"myValue" };  

Should I release dict using release or autorelease?
Or I do not own the object, so I should not release it myself?

Note: I use manual reference counting (ARC is disabled).

like image 990
Dj S Avatar asked May 12 '14 03:05

Dj S


2 Answers

No, you must not release a NSDictionary created with the literal syntax. The Clang documentation tells that dictionary literal expressions expand to +[NSDictionary dictionaryWithObjects:forKeys:count:], so you do not own the object.

No Objective-C literal expands to an expression creating an owning reference.

like image 165
zneak Avatar answered Sep 20 '22 13:09

zneak


Even if you have ARC disabled (which you should have it enabled), you do not need to release it because you do not own the object. You only own the object when you created it through alloc or new.

like image 27
TheAmateurProgrammer Avatar answered Sep 20 '22 13:09

TheAmateurProgrammer