0

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.

Ser Pounce
  • 14,196
  • 18
  • 84
  • 169

1 Answers1

4

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.

rob mayoff
  • 375,296
  • 67
  • 796
  • 848
  • thanks Rob, on point like usual. Had another question: what if I just used @property(nonatomic, retain) for both, and when I want to make copy do something like this: NSMutableAttributedString *textCopyTemp = [text mutableCopy]; textCopy = textCopyTemp; [textCopyTemp release]; – Ser Pounce Jun 19 '12 at 04:15
  • That middle statement needs to be `self.textCopy = textCopyTemp;` to avoid leaking the copy. – rob mayoff Jun 19 '12 at 04:28