1, preferences-more for user name, whether automatic login and other data storage
Many iOS apps support preferences such as saving usernames, passwords, font size, and more, andiOS offers a standard set of solutions to add preferences to your app
Each app has a nsuserdefaults instance (with only one cut) that accesses preferences , such as saving a user name, font size, automatic login
(1) Save Content Settings
1 nsuserdefaults *defaults = [Nsuserdefaults standarduserdefaults];
//Store the user name 2 [defaults setobject:@ "JFCK" forkey:@ "username"];
//Store the font size 3 [defaults setfloat:18.0f forkey:@ "text_size"];
(2) Read the last saved settings
1Nsuserdefaults *defaults =[Nsuserdefaults standarduserdefaults];2Read username The value of this key3NSString *username = [Defaults stringforkey:@"username"];4Read Text_size The value of this key
5 floatTextSize = [Defaults floatforkey:@"text_size"];
6Read Auto_login The value of this key
7BOOL autologin = [Defaults boolforkey:@"Auto_login"];
(3) Note: when setting the data, Userdefaults does not write immediately, but instead writes the cached data to the local disk according to the timestamp. So after calling the set method, it is possible that the data has not yet been written to the disk application to terminate. The above problem can be forced by calling the synchornize method to write
1 [defaults synchornize];
2. plist Storage-dictionary, array storage
(1) Data storage:
1 " get the Cache file path:
// get the Cache file path
Nssearchpathdirectory: Search for Directories
Nssearchpathdomainmask: Search scope Nsuserdomainmask: means to find on the user's phone
Expandtilde whether to expand the full path, if not expanded, the application sandbox path is ~
Store be sure to expand the path
NSString *cachepaht = Nssearchpathfordirectoriesindomains (Nscachesdirectory, Nsuserdomainmask, YES) [0];
2 "Stitching file name:
NSString *filepath = [cachepaht stringbyappendingpathcomponent:@ "Arr.plist"];
3 " write information to file:
File: Full path to files
[arr Writetofile:filepath Atomically:yes];
Example:
1- (void) Saver {2 //how to tell if an object can use plist, see if there is WriteToFile3Nsarray *arr = @[@"123",@1];4 //get the cache file path5 //nssearchpathdirectory: Search for Directories6 //nssearchpathdomainmask: Search scope Nsuserdomainmask: means to find on the user's phone7 //Expandtilde whether to expand the full path, if not expanded, the application sandbox path is ~8 //Store Be sure to expand the path9NSString *cachepaht = Nssearchpathfordirectoriesindomains (Nscachesdirectory, Nsuserdomainmask, YES) [0];Ten //Stitching file name OneNSString *filepath = [Cachepaht stringbyappendingpathcomponent:@"arr.plist"]; A //file: Full path to files - [arr Writetofile:filepath atomically:yes]; -}
Data read:
1- (void) Read {2 //get the cache file path3NSString *cachepaht = Nssearchpathfordirectoriesindomains (Nscachesdirectory, Nsuserdomainmask, YES) [0];4 //Stitching file name5NSString *filepath = [Cachepaht stringbyappendingpathcomponent:@"arr.plist"];6Nsarray *arr =[Nsarray Arraywithcontentsoffile:filepath];7NSLog (@"%@", arr);8}
3.Nskeyedarchiver Archive Solution (nscoding)
If the object is nsstring,nsdictionary,nsarray,nsdata, NSNumber and other types, you can use the Nskeyedarchiver for archiving and recovery;
Not all objects can be archived directly in this way, only objects that adhere to the nscoding protocol.
nscoding The protocol has 2 methods :
1,encodewithcoder:
This method is called every time the object is archived. In this method, you typically specify how to archive each instance variable in an object, and you can use the Encodeobject:forkey: method to archive instance variables.
2,initwithcoder:
This method is called every time the object is recovered (decoded) from the file. In this method, you typically specify how to decode the data in a file as an instance variable of an object, and you can use the Decodeobject:forkey method to decode the instance variable.
Full instance: access the contact's name, phone number information data:
1.Define attributes and declare class methods (methods of instantiating objects) normally in the header file of the data model
1 #import <foundation/ Foundation.h> 3 @interface Ickcontact:nsobject<nscoding> 4 5 @property (nonatomic, strong) NSString *name; 7 @property (nonatomic, Strong) NSString *phone; 9 + (Instancetype) Contactwithname: ( NSString *) name Andphone: (nsstring *) phone; 10 @ End
2, in the data model implementation file, in addition to implementing the instantiation of the object's class method, but also the new implementation of the object method of instantiating objects and storage properties of the method:
1 #import "ICKContact.h"2 3 @implementationickcontact4 //instantiating a data model method5+ (Instancetype) Contactwithname: (NSString *) name Andphone: (NSString *) Phone6 {7Ickcontact *contact =[[Ickcontact alloc] init];8Contact.name =name;9Contact.phone =phone;Ten returnContact ; One } A //methods to be invoked to comply with the Archiving protocol -- (void) Encodewithcoder: (Nscoder *) Acoder - { the[Acoder encodeObject:self.name Forkey:@"name"]; -[Acoder encodeObject:self.phone Forkey:@"Phone"]; - } - + //methods to be invoked to comply with the Archiving protocol --(Instancetype) Initwithcoder: (Nscoder *) Adecoder + { A //whether the super also implements the (Initwithcoder:adecoder) method depends on whether the parent class super also complies with the Nscoding protocol at if(self =[Super Init]) { -Self.name = [Adecoder decodeobjectforkey:@"name"]; -Self.phone = [Adecoder decodeobjectforkey:@"Phone"]; - } - returnSelf ; - } in - @end
3.Store data: Implement in files that need to store data
1 // 2 nsstring *cache = Nssearchpathfordirectoriesindomains ( Nscachesdirectory, Nsuserdomainmask, YES) [0 3 nsstring *path = [Cache stringbyappendingstring: @ " contacts.data " ]; 4 Writes as an array (stored in the underlying or as an object) 5 // self.contacts: Data model Array (omitted here) 6 [Nskeyedarchiver archiveRootObject:self.contacts Tofile:path];
4. Get the data:
1 nsstring *cache = Nssearchpathfordirectoriesindomains (Nscachesdirectory, Nsuserdomainmask, YES) [0 ]; 2 nsstring *path = [Cache stringbyappendingstring:@ "contacts.data"]; 3 _contacts = [Nskeyedunarchiver Unarchiveobjectwithfile:path];
iOS development, you have to know the data stored---dry