NSAttributedString for NSData No visible @interface for 'NSAttributedString' declares selector 'RTFDFromRange: documentAttributes:

I am trying to convert NSAttributedString to NSData using the RTFDFromRange method. Getting this:

 No visible @interface for 'NSAttributedString' declares the selector 'RTFDFromRange:documentAttributes: 

What is wrong with my code?

 NSAttributedString *val=self.textview.attributedText; NSData *data = [val RTFDFromRange:NSMakeRange(0, self.textview.text.length) documentAttributes:nil]; 
+6
source share
2 answers

NSAttributedString does not have a method called RTFDFromRange for iOS, but only for Mac OS X.

To convert NSAttributedString to NSData on iOS, you can try the following two approaches:

1. Using initWithData :

 NSMutableAttributedString *val = [[NSMutableAttributedString alloc] initWithData:data options:nil documentAttributes:nil error:nil]; 

2. Using NSKeyedArchiver :

 NSData *data = [NSKeyedArchiver archivedDataWithRootObject: val]; 

And to convert NSData back to string:

 NSAttributedString *val = [NSKeyedUnarchiver unarchiveObjectWithData: data]; 

This code works on both Mac and iOS.

See Apple docs here .

+9
source

This method is only available in Cocoa (OSX) as part of AppKit Additions prior to NSAttributedString .

Here's an open source category that can do what you want in iOS (but not personally tested).

+1
source

Source: https://habr.com/ru/post/973356/


All Articles