I'm just wondering how I can make a copy of an NSMutableAttributedString
. I have a property called text
that I'd like to save its contents at a certain point and revert back to it in case something happens. I've tried making a property called textCopy
where I can save it to with @property (nonatomic, copy)
but I get a runtime error when I do this:
-[NSConcreteAttributedString insertAttributedString:atIndex:]: unrecognized selector sent to instance.
How would I accomplish this?
Updated with run time error. I'm getting this anytime I set the NSMutableAttributedString to @property (nonatomic, copy)
. Not understanding why this wouldn't work, in general the copy paramter doesn't seem to work with NSMutableAttributedString whether I'm using its setter method or not.
The problem is that you've declared the property with the
copy
attribute, and are presumably using the compiler-generated setter. The compiler-generated setter sends thecopy
message to the object to make a copy. Thecopy
message makes an immutable copy. That is, it creates anNSAttributedString
, not anNSMutableAttributedString
.One way to fix this is to write your own setter that uses
mutableCopy
, like this if you're using ARC:or like this if you're using manual reference counting:
Another fix would be to make
textCopy
be anNSAttributedString
instead of anNSMutableAttributedString
, and make the rest of your code work with it as an immutable object.