How to define mutable objects?

In my application, at some point, I need to define mutable objects for their proper management. This is more complicated than it sounds.

If I use isKindOfClass:[NSMutableString class] in NSString , all lines seem mutable.

If I ask them if they respond to @selector(appendString:) or not, they all do.

If I really try to mutate them in the @try ... @catch ... @finally , the truth is revealed. However, it is very inefficient and very slow.

What would be the best course of action?

+7
source share
1 answer

If you want to ignore volatility, you can iterate through the loop and make mutable copies of each line using -mutableCopy (which, in spite of everything, creates NSMutableStrings). The opposite is true: by sending -copy each object, you are guaranteed to receive an immutable NSString. Unfortunately, interoperability is not a purely verifiable concept because NSString is a class cluster. Checking a class with this line @"__NSCFString" is shockingly unsafe because Apple has the right to change the base class at any time.

+2
source

All Articles