views:

594

answers:

1

That's somewhat confusing. NSMutableString inherits from NSString, but does that also mean that I can pass an NSMutableString anywhere safely where actually an NSString is wanted? And can I assign an NSString to an NSMutableString? How would I get an NSString out of an NSMutableString to avoid problems, if any?

+5  A: 
  1. Yes, you can pass an NSMutableString for an NSString. However, be aware that if the object you pass it to stores a reference to this object, it will "see" all the changes you make to the mutable string object. This is not always desirable.

  2. No, not possible. If you had a NSMutableString pointer pointing to an NSString and called, say, appendString: on it, the object wouldn't know how to process the call.

    (the first two are OO inclusion polymorphism basics)

  3. If you want to get a non-mutable string from a mutable one, use [mutableString copy].

Rüdiger Hanke