swift--使用UserDefaults來進行本地數據存儲

UserDefaults適合輕量級的本地客戶端存儲,存儲一個值,新值能夠覆蓋舊值,能夠重複存儲,也能夠存儲一次,而後直接從UserDefaults裏面讀取上次存儲的信息,很方便,用的時候,宏定義下,直接調用!數據庫

輕量級數據庫的話,移動端通常使用SQlite數據庫,也是一款輕量級的適合移動端設備的數據庫,具體能夠參考我之前寫的關於SQlite的博客,固然了UserDefaults根本不用管這些東西,直接就想讀取字符串同樣,直接讀取就能夠了!ui

UserDefaults支持的數據格式也不少:有:Int,Float,Double,BOOL,Array,Dictionary,甚至 Any 類型url

1. 樣例展現:spa

 

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
        }
    }

 

輸出:code

 print("用戶的UUID:\(get_uuid())")

如上圖!對象

2.其餘基本數據類型和any類型的存儲blog

 //存儲練習
        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)")

打印以下圖:rem

 3.刪除存儲對象字符串

經過removeObject()方法能夠刪除已保存的數據get

UserDefaults.standard.removeObject(forKey: "key值")
相關文章
相關標籤/搜索