Model object archiving for iPhone application development

Source: Internet
Author: User

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.

 
 
  1. @interface FourLines : NSObject <NSCoding, NSCopying> {  
  2.     NSString *field1;  
  3.     NSString *field2;  
  4.     NSString *field3;  
  5.     NSString *field4;     
  6. }  
  7.  
  8. @property (nonatomic, retain) NSString *field1;  
  9. @property (nonatomic, retain) NSString *field2;  
  10. @property (nonatomic, retain) NSString *field3;  
  11. @property (nonatomic, retain) NSString *field4;  
  12. @end  
  13. #pragma mark NSCoding  
  14. - (void)encodeWithCoder:(NSCoder *)encoder {  
  15.     [encoder encodeObject:field1 forKey:kField1Key];  
  16.     [encoder encodeObject:field2 forKey:kField2Key];  
  17.     [encoder encodeObject:field3 forKey:kField3Key];  
  18.     [encoder encodeObject:field4 forKey:kField4Key];  
  19. }  
  20. - (id)initWithCoder:(NSCoder *)decoder {  
  21.     if (self = [super init]) {  
  22.         self.field1 = [decoder decodeObjectForKey:kField1Key];  
  23.         self.field2 = [decoder decodeObjectForKey:kField2Key];  
  24.         self.field3 = [decoder decodeObjectForKey:kField3Key];  
  25.         self.field4 = [decoder decodeObjectForKey:kField4Key];  
  26.     }  
  27.     return self;  
  28. }  
  29. #pragma mark NSCopying  
  30. - (id)copyWithZone:(NSZone *)zone {  
  31.     FourLines *copy = [[[self class] allocWithZone: zone] init];  
  32.     copy.field1 = [[self.field1 copyWithZone:zone] autorelease];  
  33.     copy.field2 = [[self.field2 copyWithZone:zone] autorelease];  
  34.     copy.field3 = [[self.field3 copyWithZone:zone] autorelease];  
  35.     copy.field4 = [[self.field4 copyWithZone:zone] autorelease];  
  36.     return copy;  

Obtain an archive file

 
 
  1. - (NSString *)dataFilePath {  
  2.     NSArray *paths = NSSearchPathForDirectoriesInDomains( NSDocumentDirectory, NSUserDomainMask, YES);  
  3.     NSString *documentsDirectory = [paths objectAtIndex:0];  
  4.     return [documentsDirectory stringByAppendingPathComponent:@"archive"];  

Archive data

 
 
  1. FourLines * fourLines = [[FourLines alloc] init];
  2. FourLines. field1 = field1.text;
  3. FourLines. field2 = field2.text;
  4. FourLines. field3 = field3.text;
  5. FourLines. field4 = field4.text;
  6. // Archive data
  7. NSMutableData * data = [NSMutableData alloc] init];
  8. NSKeyedArchiver * archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData: data];
  9. [Archiver encodeObject: fourLines forKey: @ "Data"];
  10. [Archiver finishEncoding];
  11. [Data writeToFile: [self dataFilePath] atomically: YES];
  12. [FourLines release];
  13. [Archiver release];
  14. [Data release];

Obtain archive data

 
 
  1. NSString *filePath = [self dataFilePath];  
  2. if ([[NSFileManager defaultManager] fileExistsAtPath:filePath]) {  
  3.     NSData *data = [[NSMutableData alloc]  
  4.                     initWithContentsOfFile:[self dataFilePath]];  
  5.     NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc]  
  6.                                      initForReadingWithData:data];  
  7.     FourLines *fourLines = [unarchiver decodeObjectForKey:@"Data"];  
  8.     [unarchiver finishDecoding];  
  9.     field1.text = fourLines.field1;  
  10.     field2.text = fourLines.field2;  
  11.     field3.text = fourLines.field3;  
  12.     field4.text = fourLines.field4;  
  13.     [unarchiver release];  
  14.     [data release];         

Summary:IPhoneModel object for application developmentArchiveThis article is helpful to you!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.