IOS 归档 即序列化与反序列化

来源:互联网 发布:中学生编程软件 编辑:程序博客网 时间:2024/06/05 18:38

小弟很久没有更新了 最近在往IOS上靠 

IOS中的归档  即是我们所知道的序列化和反序列化

我们可以用plist来存储比较简单的数据类型 但是如果我想把自己定义的类型进行持久化呢?

这就要用到序列化了 下面贴代码

先是自定义一个自己的类  需要继承 NSCoding  接口

-------------------------------------//我是分隔线//-----------------------------------------

#import <Foundation/Foundation.h>


@interface FourLines : NSObject <NSCoding,NSCopying> {

}
@property (nonatomic,retain) NSString *field1;
@property (nonatomic,retain) NSString *field2;
@property (nonatomic,retain) NSString *field3;
@property (nonatomic,retain) NSString *field4;

@end

 

#import "FourLines.h"
#define kField1Key @"Field1"
#define kField2Key @"Field2"
#define kField3Key @"Field3"
#define kField4Key @"Field4"

@implementation FourLines

@synthesize field1;
@synthesize field2;
@synthesize field3;
@synthesize field4;

- (void) encodeWithCoder:(NSCoder *)aCoder{
 [aCoder encodeObject:field1 forKey:kField1Key];
 [aCoder encodeObject:field2 forKey:kField2Key];
 [aCoder encodeObject:field3 forKey:kField3Key];
 [aCoder encodeObject:field4 forKey:kField4Key];
}

- (id)initWithCoder:(NSCoder *)aDecoder{
 if (self = [super init]) {
  field1 = [[aDecoder decodeObjectForKey:kField1Key] retain];
  field2 = [[aDecoder decodeObjectForKey:kField2Key] retain];
  field3 = [[aDecoder decodeObjectForKey:kField3Key] retain];
  field4 = [[aDecoder decodeObjectForKey:kField4Key] retain];
 }
 return self;
}

- (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;
}

- (void)dealloc{
 [field1 release];
 [field2 release];
 [field3 release];
 [field4 release];
 [super dealloc];
}

@end

 

下面是一个controller 来实现如何持久化自定义类

 

#import <UIKit/UIKit.h>

 //#define kFilename @"data.plist"
#define kFilename @"archive"
#define kDataKey @"Data"
 //define kFilename @"dataarchiive.plist"
 //#define kDataKey @"Data"
 //#define kFilename @"data.sqlite3"

@interface PersistenceViewController : UIViewController {

}
@property (nonatomic,retain) IBOutlet UITextField *field1;
@property (nonatomic,retain) IBOutlet UITextField *field2;
@property (nonatomic,retain) IBOutlet UITextField *field3;
@property (nonatomic,retain) IBOutlet UITextField *field4;

- (NSString *)dataFilePath;
- (void)applicationWillResignActive:(NSNotification *)notification;

@end

 

 

#import "PersistenceViewController.h"
#import "FourLines.h"

@implementation PersistenceViewController

@synthesize field1;
@synthesize field2;
@synthesize field3;
@synthesize field4;


- (NSString *)dataFilePath{
 NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
 NSString *documentsDirectory = [paths objectAtIndex:0];
 NSLog(@"Document Path:%@",documentsDirectory);
 return [documentsDirectory stringByAppendingPathComponent:kFilename];
}
/*
// The designated initializer. Override to perform setup that is required before the view is loaded.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}
*/

/*
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView {
}
*/

 

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
 NSString *filePath = [self dataFilePath];
 if ([[NSFileManager defaultManager] fileExistsAtPath:filePath]) {
  /*
  NSArray *array = [[NSArray alloc] initWithContentsOfFile:filePath];
  field1.text = [array objectAtIndex:0];
  field2.text = [array objectAtIndex:1];
  field3.text = [array objectAtIndex:2];
  field4.text = [array objectAtIndex:3];
  [array release];
  */
  
   //encoding
  NSData *data = [[NSMutableData alloc] initWithContentsOfFile:filePath];
  NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:data];
  
  FourLines *fourLines = [unarchiver decodeObjectForKey:kDataKey];
  [unarchiver finishDecoding];
  
  field1.text = fourLines.field1;
  field2.text = fourLines.field2;
  field3.text = fourLines.field3;
  field4.text = fourLines.field4;
  
  [unarchiver release];
  [data release];
 }
 
 UIApplication *app = [UIApplication sharedApplication];
 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationWillResignActive:) name:UIApplicationWillResignActiveNotification object:app];
    [super viewDidLoad];
}

- (void) applicationWillResignActive:(NSNotification *)notification{
 /*
 NSMutableArray *array = [[NSMutableArray alloc] init];
 [array addObject:field1.text];
 [array addObject:field2.text];
 [array addObject:field3.text];
 [array addObject:field4.text];
 [array writeToFile:[self dataFilePath] atomically:YES];
 */
 FourLines *fourLine = [[FourLines alloc] init];
 fourLine.field1 = field1.text;
 fourLine.field2 = field2.text;
 fourLine.field3 = field3.text;
 fourLine.field4 = field4.text;
 
 NSMutableData *data = [[NSMutableData alloc] init];
 NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data];
 [archiver encodeObject:fourLine forKey:kDataKey];
 [archiver finishEncoding];
 [data writeToFile:[self dataFilePath] atomically:YES];
 [fourLine release];
 [data release];
 [archiver release];
}

// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    return YES;
}

- (void)didReceiveMemoryWarning {
 // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
 
 // Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
 // Release any retained subviews of the main view.
 // e.g. self.myOutlet = nil;
}


- (void)dealloc {
 [field1 release];
 [field2 release];
 [field3 release];
 [field4 release];
    [super dealloc];
}

@end

0 0
原创粉丝点击