swift--使用UserDefaults來進行本機資料儲存

來源:互聯網
上載者:User

標籤:int   data   http   png   cti   分享   ati   jpeg   val   

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.其他基礎資料型別 (Elementary Data Type)和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)        //UIImageObject Storage Service        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值")

 

swift--使用UserDefaults來進行本機資料儲存

相關文章

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.