Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

mdeh

macrumors 6502
Original poster
Jan 3, 2009
345
2
Hi all,
I have a problem that I cannot understand! :confused:

According to the documentation, there is a method "mutableString" which, in the NSMutableAttributedString class
Returns the character contents of the receiver as an NSMutableString object.

So, with this code

Code:
NSMutableString * s = [_mString mutableString];

I would have expected s to be a mutableString. Here is the error:

-[NSConcreteAttributedString mutableString]: unrecognized selector sent to instance 0x100474560

Changing to this code:

Code:
NSString * s = [_mString string];

eliminates this issue...but the question is why? The literature that I have looked at does not seem to have an answer.

Thanks as usual.
 
NSMutableAttributedString's methods aren't inherited by NSAttributedString. So any method specifically defined for NSMutableAttributedString will not work on NSAttributedString.

If you want a mutable string for NSAttributedString just call mutableCopy on the string.

How is _mString created?
 
NSMutableAttributedString's methods aren't inherited by NSAttributedString. So any method specifically defined for NSMutableAttributedString will not work on NSAttributedString.
How is _mString created?

Kainjow...thanks, first off.

-mString is created by by taking the textStorage property of an NSTextView, and setting an NSMutableAttributed string using an accessor thus.


Code:
-(void) setMString:(NSMutableAttributedString *) m
{
	if ( m == nil)
	{
		return;
	}
	m = [m copy];
	[_mString release];
	_mString = m;

Earlier, I removed the actual use of the setter to make it read more easily, but here is the actual call to set it.

Code:
[[self cypherCenter] setMString:[[self cypherField]textStorage]];

Where cypherCenter is the class that has the accessor setMString.


Where I am a little confused is that the method:
- (NSMutableString *)mutableString is described within the NSMutableAttributedString Class and shows that an NSMutableString is returned, which is what I thought I was doing :), but apparently not! Well, it was causing this issue. Still a little confused, but thanks for your input.
 
Code:
m = [m copy];

Try using mutableCopy here.

Have a read through the section Copying Mutable Versus Immutable Objects (at the bottom). Basically, if you call copy on a mutable object, it will return an immutable one instead. This applies to things like NSArray, NSString, NSDictionary, etc., and other classes that have a separate mutable version.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.