Difference bewteen declaring a NSString with alloc and init and assigning with @“myString”

混江龙づ霸主 提交于 2019-12-02 05:52:13
NSString *constantString = @"constantString";

String like constantString are said to be from a private(?) class NSConstantString and they are alive through all your program life. Off-course release and retain work, (in the mean that they won't give you a exception or crash) They just do nothing.

Read more here

Also you said in one of your comments that it would be a{@property(..., copy) NSString myString;But what you are showing us is a typical @property(..., retain)

AFAIK, string constants of the form @"..." are actually a child class of NSString that redefine retain and release as no-ops. This allows the compiler to store those string constants in the data segment of your executable instead of on the heap.

Is myString declared in the header-file? Like: @property(nonatomic, retain) NSString myString. If that is the case, then myString is retained. Otherwise, it's not necessary to release it.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!