开发者

Objective-C memory management: do you need to release string literals?

do you开发者_C百科 need to release something very simple this?

NSString *a = @"Hello";

//[a release];  ?

I come from a Java/C# world, and am confused about when things should be released/retained.


No. You only need to release objects you init/alloc yourself or your instance variables in your class dealloc method.


No, you do not need to release a constant NSString, though it doesn't cause any problems if you do. Constant strings are special case of the memory management system. Since their content is known at compile time, it is statically defined in the application binary itself, so it never has to be allocated or freed at runtime. For that reason, its retain and release methods are noops.

This is only true for constant NSStrings (strings that start with @), and their toll free bridged cousin, constant CFStrings (defined using the CFSTR() macro).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜