UserDefaults是iOS中提供的一套最为基础的持久化方式,一般用于存储用户的基本信息
直接上代码:
// 最简单的使用于存取
func userDefaultsSave() -> Void {
let user:UserDefaults = UserDefaults.standard
/*基础的可存储的类型*/
user.set(URL(string: "https://www.baidu.com"), forKey: "url")
user.set("anyObject", forKey: "any")
user.set(true, forKey: "bool")
user.set(Double(10), forKey: "double")
user.set(Float(10), forKey: "float")
user.set(Int(10), forKey: "int")
/*数据读取*/
let url:URL = user.url(forKey: "url")!
let any:Any = user.object(forKey: "any")!
let bool:Bool = user.bool(forKey: "bool")
let double:Double = user.double(forKey: "double")
let float:Float = user.float(forKey: "float")
let int:Int = user.integer(forKey: "int")
print("url:\(url), any:\(any), bool:\(bool), double:\(double), float:\(float), int:\(int)")
/*存储数组*/
let array:Array<Any> = ["hello",["name":"bayMax"],["one","two","three"]]