NSAttributedString到NSData没有'NSAttributedString'的可见@interface声明选择器'RTFDFromRange:documentAttributes:

amo*_*one 7 nsattributedstring nsdata ios

我正在尝试转换NSAttributedStringNSData使用RTFDFromRange方法.得到这个:

No visible @interface for 'NSAttributedString' declares the selector 'RTFDFromRange:documentAttributes:
Run Code Online (Sandbox Code Playgroud)

我的代码出了什么问题?

NSAttributedString *val=self.textview.attributedText;
    NSData *data = [val RTFDFromRange:NSMakeRange(0, self.textview.text.length) documentAttributes:nil];
Run Code Online (Sandbox Code Playgroud)

Nee*_*eku 11

NSAttributedString没有RTFDFromRange为iOS 调用的方法,但仅适用于Mac OS X.

要转换NSAttributedStringNSDataiOS,您可以尝试以下两种方法:

1.使用initWithData:

NSMutableAttributedString *val = [[NSMutableAttributedString alloc] initWithData:data options:nil documentAttributes:nil error:nil];
Run Code Online (Sandbox Code Playgroud)

2.使用NSKeyedArchiver:

NSData *data = [NSKeyedArchiver archivedDataWithRootObject: val];
Run Code Online (Sandbox Code Playgroud)

并将NSData后面的字符串转换为字符串:

NSAttributedString *val = [NSKeyedUnarchiver unarchiveObjectWithData: data];
Run Code Online (Sandbox Code Playgroud)

此代码适用于Mac和iOS.

请在此处查看Apple文档.