Convert NSMutableAttributedString to NSString

tech_human picture tech_human · Oct 11, 2013 · Viewed 43.6k times · Source

Can we not convert NSMutableAttributedString to NSString?

I have two NSMutableAttributedStrings and I am appending the 2nd string onto 1st as below:

[string1 appendAttributedString:string2];

Since I have to display string1 on a label I do:

self.label1.text = (NSString *)string1;

I am getting "unrecognized selector sent to instance" error.

Am I doing anything wrong here? Isn't this the correct way to assign a NSMutableAttributedString to text property of a label?

Answer

rmaddy picture rmaddy · Oct 11, 2013

You can't use a cast to convert an object from one type to another. Use the provided method:

label1.text = [string1 string];

Better yet, use the attributed string:

label1.attributedText = string1