0.删除存储对象
UserDefaults.standard.removeObject(forKey: "hangge")
1.创建一个userdefault
let userDefault = UserDefaults.standard
2.存储
userDefault.set("hangge.com", forKey: "Object")
let objectValue:Any? = userDefault.object(forKey: "Object")
var dictionary = ["1":"hangge.com"]
userDefault.set(dictionary, forKey: "Dictionary")
dictionary = userDefault.dictionary(forKey: "Dictionary") as! [String : String]
3.系统对象的存储和读取
//UILabel对象存储
//将对象转换成Data流
let label = UILabel()
label.text = "欢迎访问hangge.com"
let labelData = NSKeyedArchiver.archivedData(withRootObject: label)
//存储Data对象
userDefault.set(labelData, forKey: "labelData")
//UILabel对象读取
//获取Data
let objData = userDefault.data(forKey: "labelData")
//还原对象
let myLabel = NSKeyedUnarchiver.unarchiveObject(with: objData!) as? UILabel
print(myLabel)
let userDefault = UserDefaults.standard
//UIImage对象存储
//将对象转换成Data流
let image1 = UIImage(named: "apple.png")!
let image2 = UIImage(cgImage: image1.cgImage!, scale: image1.scale,
orientation: image1.imageOrientation)
let imageData = NSKeyedArchiver.archivedData(withRootObject: image2)
//存储Data对象
userDefault.set(imageData, forKey: "imageData")
//UIImage对象读取
//获取Data
let objData = userDefault.data(forKey: "imageData")
//还原对象
let myImage = NSKeyedUnarchiver.unarchiveObject(with: objData!) as? UIImage
print(myImage)