我试图使用RTFDFromRange方法将NSAttributedString转换为NSData.得到这个:
No visible @interface for 'NSAttributedString' declares the selector 'RTFDFromRange:documentAttributes:
我的代码出了什么问题?
NSAttributedString *val=self.textview.attributedText; NSData *data = [val RTFDFromRange:NSMakeRange(0,self.textview.text.length) documentAttributes:nil];
解决方法
NSAttributedString没有适用于iOS的RTFDFromRange方法,但仅适用于Mac OS X.
要在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文档here.
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。