70

I have an NSMutableString, how can I convert it to an NSString?

Regexident
  • 29,441
  • 10
  • 93
  • 100
cyclingIsBetter
  • 17,447
  • 50
  • 156
  • 241
  • 2
    From your recent (today's) question history it looks like you should get your hand one a decent book on Cocoa to get a basic idea of how things work in ObjC-world. Aaron Hillegass' books are a very good start. Also check out http://cocoadevcentral.com/ Further more: posting half a dozen slightly different versions of basically the same question (String->CSV), of which this question I guess is also related to, won't help you much. You're just spamming SO and making people eventually ignore your questions. – Regexident Apr 07 '11 at 16:09
  • Instead you should rather go and provide more info for your initial question if the provided answers didn't help you and people will try to provide more fitting answers to it. Once an answer solved your problem, give it an upvote and click the green checkmark to accept it. this way other users with the same problem can find validated solutions much, much easier and will thank you later. Also it increases your chances of actually getting answers in the first place ;) – Regexident Apr 07 '11 at 16:12
  • 1
    yes, you're right, sorry....I will try to be less oppressive – cyclingIsBetter Apr 07 '11 at 16:16

2 Answers2

107

Either via:

NSString *immutableString = [NSString stringWithString:yourMutableString];

or via:

NSString *immutableString = [[yourMutableString copy] autorelease];
//Note that calling [foo copy] on a mutable object of which there exists an immutable variant
//such as NSMutableString, NSMutableArray, NSMutableDictionary from the Foundation framework
//is expected to return an immutable copy. For a mutable copy call [foo mutableCopy] instead.

Being a subclass of NSString however you can just cast it to an NSString

NSString *immutableString = yourMutableString;

making it appear immutable, even though it in fact stays mutable.
Many methods actually return mutable instances despite being declared to return immutable ones.

Regexident
  • 29,441
  • 10
  • 93
  • 100
  • 1
    Good answer! For the last solution you don't need to typecast though, since NSMutableString inherits from NSString and you can always assign a subclass's type to a type from which it inherits without the compiler complaining. – Senseful Jul 17 '13 at 06:41
  • @Regexident can you tell me. if they simply return `NSString` (in fact is `NSMutableString` then when user modify `NSString` object, they don't know object itself will be changed, too right ? So this work will return some side effect ? Thanks :) – hqt Aug 09 '14 at 04:31
  • One cannot modify an `NSMutableString *` posing as `NSString *` without casting it back to an `NSMutableString *`. If a method's signature says it returns immutable and you cast it to mutable, then you are relying on an implementation detail and (even worse) are knowingly breaking the mutability contract and are thus responsible for any damage caused by this. It is safe as long as you play by the rules. Always (99%) code like you've never seen the content of any `.m` file other than the one you're in right now. Well, actually you should(/may) know, but not your code. – Regexident Aug 09 '14 at 08:13
4

NSMutableString is a subclass of NSString, so you could just typecast it:

NSString *string = (NSString *)mutableString;

In this case, string would be an alias of mutalbeString, but the compiler would complain if you tried to call any mutable methods on it.

Also, you could create a new NSString with the class method:

NSString *string = [NSString stringWithString:mutableString];
Jose Ibanez
  • 3,325
  • 3
  • 28
  • 33
  • can you tell me. if they simply return NSString (in fact is NSMutableString then when user modify NSString object, they don't know object itself will be changed, too right ? So this work will return some side effect ? Thanks :) – hqt Aug 09 '14 at 04:32
  • But if there is a change to the underlying mutable string (via the mutableString variable, will it in fact change the underlying value of the immutable string? If they are both pointers to the same object, it seems like it would have to. – Tom Bogle Jan 08 '18 at 19:14