Using Nsuserdefaults for local data storage
Nsuserdefaults is suitable for storing lightweight local client data, such as remembering the password function, to save a system user name, password. Using Nsuserdefaults is preferred. The next time you log in, you can read the last login information directly from the Nsuserdefaults. Generally stored data locally we can also be used in SQLite database, or the use of their own established plist files and so on, but this also has to show the creation of files, read files, very troublesome, but with nsuserdefaults do not care about these things, like reading a string, Direct reading is all you can do. Nsuserdefaults supports a wide range of data formats, including: Int,float,double,bool and even anyobject types.1, the following example demonstrates the use of Nsuserdefaults:(1) If it is the first time to run the program through the Cfuuidcreate method to generate a unique string as a user ID stored up (shape: b8ddb58d-73bf-4e39-a051-365858fc4626) (2) run back directly from the Nsuserdefaults to remove the user ID
1 classFunc Get_uuid ()string{2var UserID = Nsuserdefaults.standarduserdefaults (). Stringforkey ("Hangge")3 if(UserID! =Nil) {4 returnUserid!5}Else{6var uuid_ref =cfuuidcreate (Nil)7var uuid_string_ref =cfuuidcreatestring (Nil, uuid_ref)8var uuid:string =nsstring (format:uuid_string_ref)9Nsuserdefaults.standarduserdefaults (). SetObject (UUID, Forkey:"Hangge")Ten returnUUID One } A}
2, storage and reading of native data types
1var Userdefault =nsuserdefaults.standarduserdefaults ()2 3 //Anyobject4Userdefault.setobject ("hangge.com", Forkey:"Object")5var objectvalue:anyobject? = Userdefault.objectforkey ("Object")6 7 //int type8Userdefault.setinteger (12345, Forkey:"Int")9var intvalue = Userdefault.integerforkey ("Int")Ten One //Float Type AUserdefault.setfloat (3.2, Forkey:"Float") -var floatvalue = Userdefault.floatforkey ("Float") - the //Double Type -Userdefault.setdouble (5.2240, Forkey:"Double") -var doublevalue = Userdefault.doubleforkey ("Double") - + //BOOL Type -Userdefault.setbool (true, Forkey:"Bool") +var boolvalue = Userdefault.boolforkey ("Bool") A at //Nsurl Type -Userdefault.seturl (Nsurl (string:"http://hangge.com")!, Forkey:"Nsurl") -var urlvalue = Userdefault.urlforkey ("Nsurl") - - //nsstring Type -Userdefault.setobject ("hangge.com", Forkey:"NSString") invar nsstringvalue = Userdefault.objectforkey ("NSString") as!NSString - to //NSNumber Type +var number:nsnumber = NSNumber (int: A) -Userdefault.setobject (number, Forkey:"NSNumber") theNumber = Userdefault.objectforkey ("NSNumber") as!NSNumber * $ //Nsarray TypePanax Notoginsengvar Array:nsarray = nsarray (array: ["123","456"]) -Userdefault.setobject (Array, Forkey:"Nsarray") the + //nsdictionaryy Type Avar dictionary:nsdictionary = nsdictionary (dictionary: ["1":"hangge.com"]) theUserdefault.setobject (dictionary, Forkey:"nsdictionary") +Dictionary = Userdefault.objectforkey ("nsdictionary") as! Nsdictionary
3, storage and reading of system objects
The system object realizes the storage, needs to convert through the Archiveddatawithrootobject method to the NSData as the carrier, only then can store. The following is an example of a UIImage object:
1var Userdefault =nsuserdefaults.standarduserdefaults ()2 3 //UIImage Object Storage4 //convert an object into a nsdata stream5var image = UIImage (named:"Apple.png")6var imagedata:nsdata = Nskeyedarchiver.archiveddatawithrootobject (image!)7 //Storing NSData Objects8Userdefault.setobject (ImageData, Forkey:"ImageData")9 Ten //UIImage Object Read One //Get NSData Avar objdata:nsdata = Userdefault.objectforkey ("ImageData") as!NSData - //Restore Objects -var myimage = Nskeyedunarchiver.unarchiveobjectwithdata (objdata) as!UIImage theprintln (MyImage)
4, custom object storage and reading
If you want to store your own defined classes, you first need to implement the Nscoding protocol for that class to archive and reverse archive. That is, add the Func Encodewithcoder (_encoder:nscoder) method and the Init (coder Decoder:nscoder) method inside the class to convert the properties.
1var Userdefault =nsuserdefaults.standarduserdefaults ()2 3 //Custom Object Storage4var model = UserInfo (name:"Air Song", Phone:"3525")5 //instance object converted to NSData6var modeldata:nsdata =Nskeyedarchiver.archiveddatawithrootobject (model)7 //Storing NSData Objects8Userdefault.setobject (Modeldata, Forkey:"MyModel")9 Ten //Custom Object reads Onevar mymodeldata = Userdefault.objectforkey ("MyModel") as!NSData Avar MyModel = Nskeyedunarchiver.unarchiveobjectwithdata (mymodeldata) as! UserInfo
iOS Development-Data Persistence & use of nsuserdefaults for local data storage