Cached Storage:nssearchpathfordirectoriesindomains Save:
//The result of this method is an array. Because there are multiple paths that can be searched.Nsarray *array =nssearchpathfordirectoriesindomains (Nscachesdirectory, Nsuserdomainmask, YES); //here, we specify that the cache directory is searched, so there is only one result, remove the cache directoryNSString *cachepath = array[0]; //stitching file path, stringByAppendingPathComponent will add/re-stitch after CachePath pathNSString *filepathname = [CachePath stringbyappendingpathcomponent:@"ageplist.plist"]; //1. In a dictionary, a dictionary is stored in the plist file.Nsdictionary *dict = @{@" Age": @ -,@"name":@"Gaowei"}; //ToFile: The sandbox path to write[Dict writetofile:filepathname Atomically:yes]; //2. Write in an array, the type stored in the plist file is an arrayNsarray *dataarray = @[@ About,@"Asdfa"]; //ToFile: The sandbox path to write[DataArray Writetofile:filepathname Atomically:yes];
Read:
//The result of this method is an array. Because there are multiple paths that can be searched.Nsarray *array =nssearchpathfordirectoriesindomains (Nscachesdirectory, Nsuserdomainmask, YES); //here, we specify that the cache directory is searched, so there is only one result, remove the cache directoryNSString *cachepath = array[0]; //Stitching file PathNSString *filepathname = [CachePath stringbyappendingpathcomponent:@"ageplist.plist"]; //read the dictionary from the file, save the plist file is a dictionary, here directly fill in the path of the plist fileNsdictionary *dict =[Nsdictionary Dictionarywithcontentsoffile:filepathname]; //If you are saving an array. It is loaded from the file by an array .Nsarray *dataarray = [Nsarray arraywithcontentsoffile:filepathname];
Storage for Preferences:nsuserdefaults Save:
//Preference Settings Nsuserdefaults//The bottom layer is to enclose a dictionary, using a dictionary to generate plist files//benefit: No need to care about file name (it will be generated automatically) fast key-value pair storage.Nsuserdefaults *defautls =[Nsuserdefaults Standarduserdefaults]; [Defautls setobject:@"Gaowei"Forkey:@"name"]; [Defautls setbool:yes Forkey:@"Isbool"]; [Defautls Setinteger:5Forkey:@"Num"]; //Synchronize and write the file immediately.[Defautls Synchronize];
Read:
//What key is used to save, read the time to use what key value to take//what type to use when you save, and what type to take when taking it.NSString *str = [[Nsuserdefaults standarduserdefaults] Objectforkey:@"name"]; BOOL Isbool= [[Nsuserdefaults standarduserdefaults] Boolforkey:@"Isbool"]; Nsinteger Num= [[Nsuserdefaults standarduserdefaults] Integerforkey:@"Num"]; NSLog (@"name =%@-isbool=%d-num=%ld", Str,isbool,num);
Archived storage:nskeyedarchiver
Unlike pist storage, where preferences are stored, archives can be used to store custom classes, which can only store data for the system's classes
//get the path to the sandbox person.data write it anywhereNSString *path = Nssearchpathfordirectoriesindomains (Nscachesdirectory, Nsuserdomainmask, YES) [0]; NSString*filepathname = [path stringbyappendingpathcomponent:@"Person.data"];//Person *per = [[Person alloc] init];//per.name = @ "XMG";//per.age = ten;Student*stu =[[Student alloc] init]; Stu.name=@"GXQ"; Stu.age=Ten; Stu.height= Max; Dog*dog =[[Dog alloc] init]; Dog.name=@"Wangcai"; Stu.dog=Dog; //Archive//Archiverootobject executes this method, the underlying invokes the Encodewithcoder method of the object to be stored ,//The purpose of calling Encodewithcoder is to ask what properties of the object you want to save. How to save.[Nskeyedarchiver Archiverootobject:stu Tofile:filepathname];
Read:
Get the path to the sandbox nsstring *path = Nssearchpathfordirectoriesindomains (Nscachesdirectory, Nsuserdomainmask, YES) [0 ]; NSString *filepathname = [path Stringbyappendingpathcomponent:@ ' person.data // archive < span> unarchiveobjectwithfile executes this method, the underlying invokes the Initwithcoder method of the object to be saved, // Student *per = [Nskeyedunarchiver unarchiveobjectwithfile:filepathname];
//Note: Regardless of the custom class object "per", the inheriting class object "Stu", contains the class object "dog", as long as the archive, the implementation of Encodewithcoder, the solution will be implemented Initwithcoder//We have to comply with the nscoding agreement.-(void) Encodewithcoder: (Nscoder *) encoder{[encoder encodeObject:self.name Forkey:@"name"]; [Encoder encodeInt:self.age Forkey:@" Age"];}//called when a file is parsed.-(Instancetype) Initwithcoder: (Nscoder *) decoder{if(self =[Super Init]) {Self.name= [Decoder Decodeobjectforkey:@"name"]; Self.age= [Decoder Decodeintforkey:@" Age"]; } returnSelf ;}
iOS Development-Data storage