Search code examples
iphoneobjective-cioscocoa-touchcore-text

Problems Making a Deep Copy of NSMutableAttributedString


I'm trying to make a deep copy of an NSMutableAttributedString called text using another NSMutableAttributedString called textBackup. Both are properties set to @property (nonatomic, retain), and I create the backup as follows:

NSMutableAttributedString *textBackupTemp = [self.text mutableCopy];
self.textBackup = textBackupTemp;
[textBackupTemp release];

Then later on, when a certain scenario occurs, I need to restore the copy:

NSMutableAttributedString *textTemp = [self.textBackup mutableCopy];
self.text = textTemp;
[textTemp release];

This seems to work fine, however the next time I access text like the following:

[self.text.string characterAtIndex: self.cursor.position-1]

I get the following error:

*** Terminating app due to uncaught exception 'NSRangeException', reason: '-[__NSCFString characterAtIndex:]: Range or index out of bounds'

The string size has changed to 1 instead of 43 like it should be. What could possibly be going on here...

EDIT: Updated the problem.


Solution

  • This was just a stupid error on my part, I had put @property(nonatomic, assign) for my textBackup instance variable. I changed it to retain and now everything works.