• swift--使用UserDefaults来进行本地数据存储


    UserDefaults适合轻量级的本地客户端存储,存储一个值,新值可以覆盖旧值,可以重复存储,也可以存储一次,然后直接从UserDefaults里面读取上次存储的信息,很方便,用的时候,宏定义下,直接调用!

    轻量级数据库的话,移动端一般使用SQlite数据库,也是一款轻量级的适合移动端设备的数据库,具体可以参考我以前写的关于SQlite的博客,当然了UserDefaults根本不用管这些东西,直接就想读取字符串一样,直接读取就可以了!

    UserDefaults支持的数据格式也很多:有:Int,Float,Double,BOOL,Array,Dictionary,甚至 Any 类型

    1. 样例展示:

    func get_uuid() -> String {
            let userID = UserDefaults.standard.string(forKey: "HHGGLL")
            //判断UserDefaults中是否已经存在
            if userID != nil {
                return userID!
            }else{
                let uuid_ref = CFUUIDCreate(nil)
                let uuid_string_ref = CFUUIDCreateString(nil, uuid_ref)
                let uuid = uuid_string_ref! as String
                UserDefaults.standard.set(uuid, forKey: "HHGGLL")
                return uuid
            }
        }

    输出:

     print("用户的UUID:(get_uuid())")

    如上图!

    2.其他基本数据类型和any类型的存储

     //存储练习
            let userDefault = UserDefaults.standard
            //any
            userDefault.set("hgl001", forKey: "object")
            let objectValue:Any? = userDefault.object(forKey: "object")
            print("(objectValue as! String)")
            //int
            userDefault.set(12345, forKey: "int")
            let intValue = userDefault.integer(forKey: "int")
            print(intValue)
            //float
            userDefault.set(3.2, forKey: "float")
            let floatValue = userDefault.float(forKey: "float")
            print(floatValue)
            //double
            userDefault.set(5.2369, forKey: "double")
            let doubleValue = userDefault.double(forKey: "double")
            print(doubleValue)
            //bool
            userDefault.set(true, forKey: "bool")
            let boolValue = userDefault.bool(forKey: "bool")
            print(boolValue)
            //url
            userDefault.set(URL(string:"http://hangge.com")!, forKey: "URL")
            let urlValue = userDefault.url(forKey: "URL")
            print(urlValue)
            //string类型
            userDefault.set("hgl is a hero!", forKey: "string")
            let stringValue = userDefault.string(forKey: "string")
            print(stringValue)
            //nsnumber类型
            var number = NSNumber(value:22)
            userDefault.set(number, forKey: "number")
            number = userDefault.object(forKey: "number") as! NSNumber
            print(number)
            //array类型
            var array:Array = ["123","223"]
            userDefault.set(array, forKey: "Array")
            array = userDefault.array(forKey: "Array") as! [String]
            print(array)
            //Dictionary类型
            var dictory = ["name":"hero11223"]
            userDefault.set(dictory, forKey: "dictory")
            dictory = userDefault.dictionary(forKey: "dictory") as! [String:String]
            print(dictory)
            //系统对象的存储与读取
            let userDeafaults = UserDefaults.standard
            //存储对象
            let label = UILabel()
            label.text = "天空飘来五个字"
            let labelData = NSKeyedArchiver.archivedData(withRootObject: label)
            //存储data对象
            userDefault.set(labelData, forKey: "labelData")
            
            //对象读取
            //获取data
            let objData = userDefault.data(forKey: "labelData")
            //还原对象
            let mylabel = NSKeyedUnarchiver.unarchiveObject(with: objData!) as? UILabel
            print(mylabel)
            //UIImage对象存储
            let image1 = UIImage(named:"1.jpeg")
            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 objDatas = userDefault.data(forKey: "imageData")
            //还原对象
            let myImg = NSKeyedUnarchiver.unarchiveObject(with: objDatas!)
            print("输出是(myImg)")

    打印如下图:

     3.删除存储对象

    通过removeObject()方法可以删除已保存的数据

    UserDefaults.standard.removeObject(forKey: "key值")
  • 相关阅读:
    IIs和ftp
    java中HashMap重要性质和优化总结
    深入理解mysql的left join(真的很深入)
    windows bat批处理语法简析
    遍历hashMap的两种方式
    Java开发实践 集合框架 全面分析
    MySQL中日期与时间类型
    CentOS6.5下Redis安装与配置
    查看redis进程
    Web阶段:第七章:Tomcat服务器
  • 原文地址:https://www.cnblogs.com/hero11223/p/7655308.html
Copyright © 2020-2023  润新知