Obj-C: [NSString stringWithString:@“string”] vs. @“string”

旧时模样 提交于 2019-11-28 13:17:47

Actually there is no difference. [NSString stringWithString: str] just does nothing and returns str if str is immutable already.

There's no difference other than the extra key strokes needed. In fact, with a constant string as a parameter (or an immutable string) you just get another pointer to the parameter.

The main use of the method is in subclasses:

[NSMutableString stringWithString: @"fdghdfjl"];

will give you a mutable autoreleased copy of the original.

One thing to note about stringWithString: is that it will throw an exception if the source string is nil.

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