问题
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.
回答1:
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 the copy
message to the object to make a copy. The copy
message makes an immutable copy. That is, it creates an NSAttributedString
, not an NSMutableAttributedString
.
One way to fix this is to write your own setter that uses mutableCopy
, like this if you're using ARC:
- (void)setTextCopy:(NSMutableAttributedString *)text {
textCopy = [text mutableCopy];
}
or like this if you're using manual reference counting:
- (void)setTextCopy:(NSMutableAttributedString *)text {
[textCopy release];
textCopy = [text mutableCopy];
}
Another fix would be to make textCopy
be an NSAttributedString
instead of an NSMutableAttributedString
, and make the rest of your code work with it as an immutable object.
来源:https://stackoverflow.com/questions/11084655/how-to-copy-a-nsmutableattributedstring