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

霸气de小男生 提交于 2019-12-05 22:55:39

问题


I am trying to convert NSAttributedString to NSData using 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];

回答1:


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

To convert NSAttributedString to NSData in iOS, you can try these 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 the NSData back to a string:

NSAttributedString *val = [NSKeyedUnarchiver unarchiveObjectWithData: data];

This code works on both Mac and iOS.

See the Apple docs here.




回答2:


That method is only available under Cocoa (OSX) as it's part of the AppKit Additions to NSAttributedString.

Here's an open source category that might do what you want under iOS (not personally tested, however).



来源:https://stackoverflow.com/questions/25134707/nsattributedstring-to-nsdata-no-visible-interface-for-nsattributedstring-decl

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!