Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I've seen people do something like [NSString stringWithString:@"some string"]. Why not just do @"some string"?

For an example, look at the facebook-ios-sdk.

+[NSString stringWithString:] -- what's the point? is a similar question, but none of the answers address [NSString stringWithString:@"some string"] vs. @"some string".

like image 977
ma11hew28 Avatar asked Jun 08 '11 14:06

ma11hew28


2 Answers

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

like image 144
Sven Avatar answered Oct 27 '22 12:10

Sven


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.

like image 29
JeremyP Avatar answered Oct 27 '22 13:10

JeremyP