Search code examples
iosobjective-cnsstringuilabelnsmutableattributedstring

Convert NSMutableAttributedString to NSString


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?


Solution

  • 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