+[NSString stringWithString:] — what's the poi

2019-01-09 06:06发布

As NSString strings are immutable, what is the value of the stringWithString: class method?

I get the utility when used with NSMutableString, I just didn't see the utility with the NSString class.

7条回答
等我变得足够好
2楼-- · 2019-01-09 06:48

FYI, now that we are compiling with ARC enabled, you don't have to manually release at all, ARC will insert the release calls during compile time. So how is it still different? stringWithString is still added to the autorelease pool which gets drained sometime in the future (unless you created your own autorelease pool). initWithString will have a release call right before the function ends, so if you didn't retain it somewhere in the method, you can be sure that the string is destroyed by the end of the function call. This gives you a tighter control on the memory management as opposed to using autoreleased objects.

查看更多
登录 后发表回答