- 源代码下载链接:06-归档普通对象.zip
34.2 KB // MJPerson.h
- //
- // MJPerson.h
- // 06-归档普通对象
- //
- // Created by apple on 13-12-11.
- // Copyright (c) 2013年itcast. All rights reserved.
- //
- #import<Foundation/Foundation.h>
- @interfaceMJPerson : NSObject <NSCoding>
- @property(nonatomic,copy) NSString *name;
- @property(nonatomic,assign)intage;
- @property(nonatomic,assign)doubleheight;
- @end
// MJPerson.m
- //
- // MJPerson.m
- // 06-归档普通对象
- //
- // Created by apple on 13-12-11.
- // Copyright (c) 2013年itcast. All rights reserved.
- //
- #import"MJPerson.h"
- @implementationMJPerson
- #pragma mark将对象归档的时候会调用(将对象写入文件之前会调用)
- //在这个方法说清楚:
- // 1.哪些属性需要存储
- // 2.怎样存储这些属性
- - (void)encodeWithCoder:(NSCoder *)encoder
- {
- //将_name属性值进行编码(会将_name的值存进文件)
- [encoder encodeObject:_name forKey:@"name"];
- [encoder encodeInt:_age forKey:@"age"];
- [encoder encodeDouble:_height forKey:@"height"];
- NSLog(@"Person---encodeWithCoder-----");
- }
- #pragma mark当从文件中解析对象时调用
- //在这个方法说清楚:
- // 1.哪些属性需要解析(读取)
- // 2.怎样解析(读取)这些属性
- - (id)initWithCoder:(NSCoder *)decoder
- {
- NSLog(@"Person---initWithCoder-----");
- if(self = [super init]) {
- _name = [decoder decodeObjectForKey:@"name"];
- _age = [decoder decodeIntForKey:@"age"];
- _height = [decoder decodeDoubleForKey:@"height"];
- }
- return self;
- }
- @end
// MJStudent.h
- //
- // MJStudent.h
- // 06-归档普通对象
- //
- // Created by apple on 13-12-11.
- // Copyright (c) 2013年itcast. All rights reserved.
- //
- #import"MJPerson.h"
- @interfaceMJStudent : MJPerson
- @property(nonatomic,copy) NSString *no;
- @end
// MJStudent.m
- //
- // MJStudent.m
- // 06-归档普通对象
- //
- // Created by apple on 13-12-11.
- // Copyright (c) 2013年itcast. All rights reserved.
- //
- #import"MJStudent.h"
- @implementation MJStudent
- - (void)encodeWithCoder:(NSCoder *)encoder
- {
- [super encodeWithCoder:encoder];
- [encoder encodeObject:_no forKey:@"no"];
- NSLog(@"MJStudent---encodeWithCoder-----");
- }
- //本文永久链接,转载请注明出处:http://www.cnblogs.com/ChenYilong/p/3490624.html
- - (id)initWithCoder:(NSCoder *)decoder
- {
- if(self= [superinitWithCoder:decoder]) {
- NSLog(@"MJStudent---encodeWithCoder-----");
- _no = [decoder decodeObjectForKey:@"no"];
- }
- returnself;
- }
- @end
// MJViewController.h
- //
- // MJViewController.h
- // 06-归档普通对象
- //
- // Created by apple on 13-12-11.
- // Copyright (c) 2013年itcast. All rights reserved.
- //
- #import<UIKit/UIKit.h>
- @interfaceMJViewController : UIViewController
- - (IBAction)save;
- - (IBAction)read;
- @end
// MJViewController.m
- //
- // MJViewController.m
- // 06-归档普通对象
- //
- // Created by apple on 13-12-11.
- // Copyright (c) 2013年itcast. All rights reserved.
- //
- #import"MJViewController.h"
- #import"MJPerson.h"
- #import"MJStudent.h"
- @interfaceMJViewController ()
- @end
- @implementationMJViewController
- - (void)viewDidLoad
- {
- [superviewDidLoad];
- // Do any additional setup after loading the view, typically from a nib.
- }
- - (void)didReceiveMemoryWarning
- {
- [superdidReceiveMemoryWarning];
- // Dispose of any resources that can be recreated.
- }
- - (IBAction)save {
- // MJPerson *p = [[MJPerson alloc] init];
- // p.name = @"jack";
- // p.age = 10;
- // p.height = 1.55;
- //
- // NSString *path = @"/Users/apple/Desktop/person.data";
- // //归档
- // [NSKeyedArchiver archiveRootObject:p toFile:path];
- MJStudent *stu = [[MJStudent alloc] init];
- stu.name =@"rose";
- stu.age =20;
- stu.height =1.75;
- stu.no =@"110";
- NSString *path =@"/Users/apple/Desktop/person.data";
- [NSKeyedArchiver archiveRootObject:stu toFile:path];
- }
- - (IBAction)read {
- NSString *path =@"/Users/apple/Desktop/person.data";
- MJStudent *stu = [NSKeyedUnarchiver unarchiveObjectWithFile:path];
- NSLog(@"%@ - %d - %f- %@", stu.name, stu.age, stu.height, stu.no);
- ////本文永久链接,转载请注明出处:http://www.cnblogs.com/ChenYilong/p/3490624.html
- // //读档(反归档)
- // MJPerson *p = [NSKeyedUnarchiver unarchiveObjectWithFile:path];
- //
- // NSLog(@"%@ - %d - %f", p.name, p.age, p.height);
- }
- @end