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:
Yes, you can pass an
NSMutableString
for anNSString
. 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.No, not possible. If you had a
NSMutableString
pointer pointing to anNSString
and called, say,appendString:
on it, the object wouldn't know how to process the call.(the first two are OO inclusion polymorphism basics)
If you want to get a non-mutable string from a mutable one, use
[mutableString copy]
.
Rüdiger Hanke
2009-08-30 18:37:20