One, plist file storage
Get files
NSString *path =*filename = [path stringbyappendingpathcomponent:@ "123.plist"];
Storing data
Nsarray *array = @[@"123"@"456"@ "789 " ]; [Array writetofile:filename atomically:yes];
Reading data
Nsarray *result = [Nsarray arraywithcontentsoffile:filename]; NSLog (@ "%@", result);
Description: Use the writetofile:atomically: method when storing, use the Arraywithcontentsoffile: method when reading.
Second, preference settings
How to use
//1. Get nsuserdefaults fileNsuserdefaults *userdefaults =[Nsuserdefaults standarduserdefaults];//2. Writing content to a file[Userdefaults SetObject:@"AAA"Forkey:@"a"]; [Userdefaults setbool:yes Forkey:@"Sex"]; [Userdefaults Setinteger: +Forkey:@" Age"];//2.1 Sync now[Userdefaults synchronize];//3. Reading filesNSString *name = [Userdefaults objectforkey:@"a"]; BOOL Sex= [Userdefaults Boolforkey:@"Sex"]; Nsinteger Age= [Userdefaults Integerforkey:@" Age"]; NSLog (@"%@,%d,%ld", name, sex, age);
Instructions for use
- Preferences are specific configuration information that is used to save the application.
- Preferences will save all the data in the same file. A plist file that is named under this application package name in the preference directory.
Third, Nskeyedarchiver (archive)
1. Follow the agreement, set the properties
// 1. Follow the nscoding agreement @interface person:nsobject<nscoding> //2. Set Property @property (Strong, nonatomic) UIImage * *name; @property (assign, nonatomic) Nsinteger age; @end
2. Implement the Protocol method
-(ID) Initwithcoder: (Nscoder *) Adecoder {if([Super init]) {Self.avatar= [Adecoder Decodeobjectforkey:@"Avatar"]; Self.name= [Adecoder Decodeobjectforkey:@"name"]; Self.age= [Adecoder Decodeintegerforkey:@" Age"]; } returnSelf ; } //Archive- (void) Encodewithcoder: (Nscoder *) Acoder {[Acoder encodeObject:self.avatar forkey:@"Avatar"]; [Acoder encodeObject:self.name Forkey:@"name"]; [Acoder encodeInteger:self.age Forkey:@" Age"]; }
3. Use
(1) The need to archive objects is the factory method that calls Nskeyedarchiver Archiverootobject:tofile: Method
(2) A factory method that calls Nskeyedunarchiver is called from the file to be the object of the document Unarchiveobjectwithfile: you can
Description
- If the class that needs to be archived is a subclass of a custom class, you need to implement the archive and file method of the parent class before archiving and reconciling the files. [Super Encodewithcoder:acoder] and [Super Initwithcoder:adecoder] method;
- Archiving is another form of serialization in iOS, as long as objects that follow the Nscoding protocol can be serialized through it. Because the majority of foundation and cocoa touch classes that support storage data follow the Nscoding protocol
IOS Data Persistence