如何在NSUserDefaults中使用存储和使用NSMutableAttributedString [英] How to use store and use an NSMutableAttributedString in NSUserDefaults
本文介绍了如何在NSUserDefaults中使用存储和使用NSMutableAttributedString的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想创建一个属性字符串,然后将其存储在NSUserDefaults
中,然后再次访问它,并将属性字符串分配给textView.attributedText
.我该怎么办?预先感谢.
I want to create an attributed string, then store it in NSUserDefaults
, and then access it again and assign the attributed string to textView.attributedText
. How do I go about this? Thanks in advance.
我不了解很多目标c,因此我无法参考此答案
I don't know a lot of objective c, so I could not refer to this answer
推荐答案
您必须将NSMutableAttributedString
转换为NSData
,然后将其存储在NSUserDefaults
中.
You have to convert your NSMutableAttributedString
into NSData
then you store it in NSUserDefaults
.
// Convert into NSData
let data = NSKeyedArchiver.archivedDataWithRootObject(distanceMutableAttributedString)
NSUserDefaults.standardUserDefaults().setObject(data, forKey: "yourStringIntoData")
// Convert your NSData to NSMutableAttributedString
let yourStringInData = NSUserDefaults.standardUserDefaults().objectForKey("yourStringIntoData") as? NSData
let newStr = NSKeyedUnarchiver.unarchiveObjectWithData(yourStringInData!) as? NSMutableAttributedString
// Assign it to your textView
textView.attributedText = newStr
这篇关于如何在NSUserDefaults中使用存储和使用NSMutableAttributedString的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文