+[NSString stringWithString:] -- what's the point?

16,854

Solution 1

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.

Solution 2

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)

Solution 3

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.

Solution 4

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]

Solution 5

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.

Share:
16,854

Related videos on Youtube

jeff7091
Author by

jeff7091

https://www.linkedin.com/in/enderwick https://medium.com/@jeff7091

Updated on August 18, 2020

Comments

  • jeff7091
    jeff7091 almost 4 years

    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.

  • Tim
    Tim over 14 years
    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.
  • Roland Rabien
    Roland Rabien over 14 years
    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.
  • Jens Ayton
    Jens Ayton over 14 years
    One minor distinction is that [[nil copy] autorelease] will return nil, but [NSString stringWithString:nil] will throw an exception.
  • ma11hew28
    ma11hew28 almost 13 years
  • FreeAsInBeer
    FreeAsInBeer over 11 years
    @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.