对模型对象进行归档
2012-05-02 15:12
162 查看
FourLines.h
FourLines.m
PersistenceViewController.h
PersistenceViewController.m
#import <Foundation/Foundation.h> #import <UIKit/UIKit.h> @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
FourLines.m
#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; /** 将对象编码到文档中 */ #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]) { field1 = [[decoder decodeObjectForKey:kField1Key] retain]; field2 = [[decoder decodeObjectForKey:kField2Key] retain]; field3 = [[decoder decodeObjectForKey:kField3Key] retain]; field4 = [[decoder decodeObjectForKey:kField4Key] retain]; } return self; } #pragma mark - #pragma mark NSCopying -(void)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; } @end
PersistenceViewController.h
#import <UIKit/UIKit.h> #define kFileName @"data.list" #define kFileName @"archive" #define kDataKey @"Data" @interface PersistenceViewController : UIViewController { UITextField *field1; UITextField *field2; UITextField *field3; UITextField *field4; } @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
PersistenceViewController.m
#import "PersistenceViewController.h" #import "FourLines.h" @implementation PersistenceViewController @synthesize field1; @synthesize field2; @synthesize field3; @synthesize field4; //用于返回数据文件的完整路径名,它通过查找Document目录并对对其附加kFilename累执行该操作。 -(NSString *)dataFilePath{ NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentsDirectory = [paths objectAtIndex:0]; return [documentsDirectory stringByAppendingFormat:kFileName]; } #pragma mark - /* // 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. //在该方法中,我们做了一下操作 /** 1,检查数据文件是否存在 2,数据文件存在,使用该引用订阅UIApplicationWillReSignActiveNotification. 3,我们使用NSNotificationCenter的默认实例,以及addObserver:select:name:object方法。我们传递一个observer即self、这就意味着 PersistenceViewController是需要通知的对象。 */ - (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]; NSData *data = [[NSMutableData alloc] initWithContentsOfFile:[self dataFilePath]]; NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:data]; FourLines *fourLine = [unarchiver decodeObjectForKey:kDataKey]; [unarchiver finishDecoding]; field1.text = fourLine.field1; field2.text = fourLine.field2; field3.text = fourLine.field3; field4.text = fourLine.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]; [array release]; 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]; [archiver release]; [data release]; } /* // Override to allow orientations other than the default portrait orientation. - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { // Return YES for supported orientations return (interfaceOrientation == UIInterfaceOrientationPortrait); } */ - (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; self.field1 = nil; self.field2 = nil; self.field3 = nil; self.field4 = nil; [super viewDidUnload]; } - (void)dealloc { [field1 release]; [field2 release]; [field3 release]; [field4 release]; [super dealloc]; } @end
相关文章推荐
- iOS 对模型对象进行归档
- IOS种四种持有化数据方式——(2)对模型对象进行归档
- ios中 继承对象模型的归档实现
- iOS 自定义对象及子类及模型套模型的拷贝、归档存储的通用代码
- SharePoint服务器端对象模型 之 使用CAML进行数据查询
- SharePoint服务器端对象模型 之 使用CAML进行数据查询(Part 2)
- iphone开发中数据持久化之——模型对象归档(二)
- SharePoint服务器端对象模型 之 使用CAML进行数据查询(Part 4)
- 实现数据模型缓存 可以用NSKeyedArchiver类来实现数据模型缓存。为了把模型对象用NSKeyedArchiver归档,模型类需要遵循NSCoding协议。
- SharePoint服务器端对象模型 之 使用CAML进行数据查询(Part 3)
- 使用SharePoint 2010 客户端对象模型进行文档库及文档的操作
- ios数据存储之--对模型对象归档(NSKeyedArchiver、NSKeyedUnarchiver)
- ios ---对模型对象归档(NSKeyedArchiver、NSKeyedUnarchiver)
- iphone开发中数据持久化之——模型对象归档(二)
- thinkphp5使用Db和对象模型进行取值
- iOS开发-进阶:数据存储之-对模型对象归档(NSKeyedArchiver、NSKeyedUnarchiver)
- ios数据存储之--对模型对象归档(NSKeyedArchiver、NSKeyedUnarchiver)
- SharePoint服务器端对象模型 之 使用LINQ进行数据访问操作(Part 1)
- ios数据存储之--对模型对象归档(NSKeyedArchiver、NSKeyedUnarchiver)
- 处理模型——对小而快的对象使用Ray-Traced进行碰撞检测