NSAttributedString到NSData'NSAttributedString'的可见@interface声明选择器'RTFDFromRange:documentAttributes: [英] NSAttributedString to NSData No visible @interface for 'NSAttributedString' declares the selector 'RTFDFromRange:documentAttributes:

查看:71
本文介绍了NSAttributedString到NSData'NSAttributedString'的可见@interface声明选择器'RTFDFromRange:documentAttributes:的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 RTFDFromRange 方法将 NSAttributedString 转换为 NSData .得到这个:

 'NSAttributedString'的可见@interface声明选择器'RTFDFromRange:documentAttributes: 

我的代码有什么问题?

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

解决方案

NSAttributedString 对于iOS而言,没有名为 RTFDFromRange 的方法,但仅对于Mac OSX./p>

要将iOS中的 NSAttributedString 转换为 NSData ,您可以尝试以下两种方法:

1.使用 initWithData :

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

2.使用 NSKeyedArchiver :

  NSData * data = [NSKeyedArchiver archivedDataWithRootObject:val]; 

并将 NSData 转换回字符串:

  NSAttributedString * val = [NSKeyedUnarchiver unarchiveObjectWithData:data]; 

此代码在Mac和iOS上均可使用.

请参阅Apple文档 解决方案

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.

这篇关于NSAttributedString到NSData'NSAttributedString'的可见@interface声明选择器'RTFDFromRange:documentAttributes:的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆