19

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.

jscs
  • 63,694
  • 13
  • 151
  • 195
jeff7091
  • 957
  • 2
  • 13
  • 25

7 Answers7

21

You might have a NSMutableString (or some home-grown NSString subclass) that you want to duplicate.

NSMutableString *buffer = [NSMutableString string];
// do something with buffer
NSString *immutableStringToKeepAround = [NSString stringWithString:buffer];

Of course, you can also just make a copy:

NSMutableString *buffer = [NSMutableString string];
// do something with buffer
NSString *immutableStringToKeepAround = [[buffer copy] autorelease];

but you own the copy and must release or autorelease it.

Don McCaughey
  • 9,532
  • 3
  • 30
  • 36
  • 13
    One minor distinction is that [[nil copy] autorelease] will return nil, but [NSString stringWithString:nil] will throw an exception. – Jens Ayton Oct 24 '09 at 06:11
9

As "Andy" points out in #318666, it's related to memory management, quoting:

The difference between initWithString and stringWithString is that stringWithString returns an auto-released pointer. This means that you don't need to release it specifically, since that will be taken care of next time that the auto-release pool cleans up any auto-released pointers.

initWithString, on the other hand, returns a pointer with a retain count of 1 - you do need to call release on that pointer, or else it would result in a memory leak.

(Taken from here)

Community
  • 1
  • 1
Federico Builes
  • 4,939
  • 4
  • 34
  • 48
1

Returns a string created by copying the characters from another given string

[NSString stringWithString:@"some string"]

It is equivalent to

[[[NSString alloc] initWithString:@"some string"] autorelease]
Alex
  • 2,438
  • 23
  • 30
  • 1
    [Why not just do `@"some string"` though?](http://stackoverflow.com/questions/6280849/nsstring-stringwithstringsome-string-vs-some-string) – ma11hew28 Jun 08 '11 at 14:57
1

Also, if you have a pointer to an NSString, it may actually be a subclass of NSString like NSMutableString. So, if you want to store the string and be guaranteed that it doesn't change, you should make a copy of it, hence stringWithString exists.

Roland Rabien
  • 8,750
  • 7
  • 50
  • 67
  • 2
    Not sure this is quite valid - since, as you point out, NSMutableString is a subclass of NSString, you can actually have something like `NSString *string = @"str"; NSMutableString *mStr = [NSMutableString stringWithString:string];` and get a mutable string back. – Tim Oct 23 '09 at 23:26
  • I meant if you have a method like -(void) setName:(NSString *)name; you don't actually know if name is an NSString or a subclass. So, you may want to make a copy of it, so it's value doesn't change behind your back. – Roland Rabien Oct 24 '09 at 01:32
  • @FigBug: But that could be intended functionality. I believe the proper way of achieving what you're intending is to use a property with the `copy` attribute. – FreeAsInBeer Nov 12 '12 at 15:54
1

As another use case, if (for whatever reason) you create your own subclass of NSString or NSMutableString, stringWithString: provides a handy way to instantiate it with an instance of either NSString, NSMutableString, or MyCustomString.

Peter Hosey
  • 95,783
  • 15
  • 211
  • 370
0

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.

ninjaneer
  • 6,951
  • 8
  • 60
  • 104
0

I often use +stringWithString: when I need to create an NSMutableString but start it with an initial value. For example:

NSMutableString * output = [NSMutableString stringWithString:@"<ul>"];
for (NSString * item in anArray) {
  [output appendFormat:@"<li>%@</li>", item];
}
[output appendString:@"</ul>"];
Dave DeLong
  • 242,470
  • 58
  • 448
  • 498