7

While doing parsing I'm converting HTML string data to attributed string text using below lines of code,

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

but my app gives bad access I'm getting an error,

[NSHTMLWebDelegate retain]: message sent to deallocated instance 0x7fa9fe027130

1
  • I had also got the same issue :(
    – Nayan
    Jul 24, 2015 at 9:11

3 Answers 3

2
[NSAttributedString alloc] initWithData:[htmlString dataUsingEncoding:NSUTF8StringEncoding] options:@{NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType, NSCharacterEncodingDocumentAttribute: @(NSUTF8StringEncoding)} documentAttributes:nil error:nil];

But the app will crash if you are running it on ios 8.1,2 or 3. To avoid the crash what you can do is : run this in a queue. So that it always be on main thread.

1

if your are releasing strPlainText and also using ARC in your project then no need to write release for strPlainText

1
  • i have not written single code of line for release of strPlaintext & my project is ARC.
    – Kalyani
    Jul 23, 2015 at 10:17
1

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 have to set str to your obj.strPlainText like obj.attributedString = str;

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.