Bad access error when converting to NSMutableattributed string

In the analysis, I convert the HTML string data into an attribute of the string text using lines of code,

obj.strPlainText=[[NSMutableAttributedString alloc] initWithData:[obj.strContent dataUsingEncoding:NSUTF8StringEncoding] options:@{NSDocumentTypeDocumentAttribute:NSHTMLTextDocumentType, NSCharacterEncodingDocumentAttribute:@(NSUTF8StringEncoding)} documentAttributes:nil error:nil]; 

but my application gives poor access I get an error message,

[NSHTMLWebDelegate save]: message sent to the freed instance 0x7fa9fe027130

+6
source share
3 answers
 [NSAttributedString alloc] initWithData:[htmlString dataUsingEncoding:NSUTF8StringEncoding] options:@{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType, NSCharacterEncodingDocumentAttribute: @(NSUTF8StringEncoding)} documentAttributes:nil error:nil]; 

But the application will crash if you use it on ios 8.1,2 or 3. To avoid a crash, you can: run it in the queue. So he will always be in the main stream.

+2
source

if you release strPlainText and also use ARC in your project, then you do not need to write release for strPlainText

+1
source

Using this will solve your problem, I think: (if it is 7.0 +)

 NSError* error; NSMutableAttributedString* str = [[NSMutableAttributedString alloc] initWithData:[source dataUsingEncoding:NSUTF8StringEncoding] options:@{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType, NSCharacterEncodingDocumentAttribute: [NSNumber numberWithInt:NSUTF8StringEncoding]} documentAttributes:nil error:&error]; 

You need to set str to obj.strPlainText as obj.attributedString = str;

+1
source

All Articles