IPhoneModel object for application developmentArchiveIs the content to be introduced in this article, mainly to introduceArchiveIt refers to another type of serialization, but it is a more general type that can be implemented by any object. Its function is to store data persistently. Let's take a look at the details.
The object must implement the NSCodeing protocol and the NSCopying protocol.
- @interface FourLines : NSObject <NSCoding, NSCopying> {
- NSString *field1;
- NSString *field2;
- NSString *field3;
- NSString *field4;
- }
-
- @property (nonatomic, retain) NSString *field1;
- @property (nonatomic, retain) NSString *field2;
- @property (nonatomic, retain) NSString *field3;
- @property (nonatomic, retain) NSString *field4;
- @end
- #pragma mark NSCoding
- - (void)encodeWithCoder:(NSCoder *)encoder {
- [encoder encodeObject:field1 forKey:kField1Key];
- [encoder encodeObject:field2 forKey:kField2Key];
- [encoder encodeObject:field3 forKey:kField3Key];
- [encoder encodeObject:field4 forKey:kField4Key];
- }
- - (id)initWithCoder:(NSCoder *)decoder {
- if (self = [super init]) {
- self.field1 = [decoder decodeObjectForKey:kField1Key];
- self.field2 = [decoder decodeObjectForKey:kField2Key];
- self.field3 = [decoder decodeObjectForKey:kField3Key];
- self.field4 = [decoder decodeObjectForKey:kField4Key];
- }
- return self;
- }
- #pragma mark NSCopying
- - (id)copyWithZone:(NSZone *)zone {
- FourLines *copy = [[[self class] allocWithZone: zone] init];
- copy.field1 = [[self.field1 copyWithZone:zone] autorelease];
- copy.field2 = [[self.field2 copyWithZone:zone] autorelease];
- copy.field3 = [[self.field3 copyWithZone:zone] autorelease];
- copy.field4 = [[self.field4 copyWithZone:zone] autorelease];
- return copy;
- }
Obtain an archive file
- - (NSString *)dataFilePath {
- NSArray *paths = NSSearchPathForDirectoriesInDomains( NSDocumentDirectory, NSUserDomainMask, YES);
- NSString *documentsDirectory = [paths objectAtIndex:0];
- return [documentsDirectory stringByAppendingPathComponent:@"archive"];
- }
Archive data
- FourLines * fourLines = [[FourLines alloc] init];
- FourLines. field1 = field1.text;
- FourLines. field2 = field2.text;
- FourLines. field3 = field3.text;
- FourLines. field4 = field4.text;
- // Archive data
- NSMutableData * data = [NSMutableData alloc] init];
- NSKeyedArchiver * archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData: data];
- [Archiver encodeObject: fourLines forKey: @ "Data"];
- [Archiver finishEncoding];
- [Data writeToFile: [self dataFilePath] atomically: YES];
- [FourLines release];
- [Archiver release];
- [Data release];
Obtain archive data
- NSString *filePath = [self dataFilePath];
- if ([[NSFileManager defaultManager] fileExistsAtPath:filePath]) {
- NSData *data = [[NSMutableData alloc]
- initWithContentsOfFile:[self dataFilePath]];
- NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc]
- initForReadingWithData:data];
- FourLines *fourLines = [unarchiver decodeObjectForKey:@"Data"];
- [unarchiver finishDecoding];
- field1.text = fourLines.field1;
- field2.text = fourLines.field2;
- field3.text = fourLines.field3;
- field4.text = fourLines.field4;
- [unarchiver release];
- [data release];
- }
Summary:IPhoneModel object for application developmentArchiveThis article is helpful to you!