NSCoding 跟其他存储方式略有不同,他可以存储对象
对象存储的条件是: 对象需要遵守 NSCoding 协议
存储的时候需要 调用 encodeWithCoder 方法读取的时候需要调用initWithCoder 方法[NSKeyedArchiver archiveRootObject:stu toFile:path]; 存储NSKeyedUnarchiver unarchiveObjectWithFile:path 读取
对象代码
[objc]
- (IBAction)save {
// 1.新的模型对象
MJStudent *stu = [[MJStudent alloc] init];
stu.no = @"42343254";
stu.age = 20;
stu.height = 1.55;
// 2.归档模型对象
// 2.1.获得Documents的全路径
NSString *doc = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
// 2.2.获得文件的全路径
NSString *path = [doc stringByAppendingPathComponent:@"stu.data"];
// 2.3.将对象归档
[NSKeyedArchiver archiveRootObject:stu toFile:path];
}
- (IBAction)read {
// 1.获得Documents的全路径
NSString *doc = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
// 2.获得文件的全路径
NSString *path = [doc stringByAppendingPathComponent:@"stu.data"];
// 3.从文件中读取MJStudent对象
MJStudent *stu = [NSKeyedUnarchiver unarchiveObjectWithFile:path];
NSLog(@"%@ %d %f", stu.no, stu.age, stu.height);
}
<EMBED id=ZeroClipboardMovie_3 height=18 name=ZeroClipboardMovie_3 type=application/x-shockwave-flash align=middle pluginspage=http://www.macromedia.com/go/getflashplayer width=18 src=http://static.blog.csdn.net/scripts/ZeroClipboard/ZeroClipboard.swf wmode="transparent" flashvars="id=3&width=18&height=18" allowfullscreen="false" allowscriptaccess="always" bgcolor="#ffffff" quality="best" menu="false" loop="false">
[objc]
保存读取#import "MJStudent.h"
@interface MJStudent()
@end
@implementation MJStudent
/**
* 将某个对象写入文件时会调用
* 在这个方法中说清楚哪些属性需要存储
*/
- (void)encodeWithCoder:(NSCoder *)encoder
{
[encoder encodeObject:self.no forKey:@"no"];
[encoder encodeInt:self.age forKey:@"age"];
[encoder encodeDouble:self.height forKey:@"height"];
}
/**
* 从文件中解析对象时会调用
* 在这个方法中说清楚哪些属性需要存储
*/
- (id)initWithCoder:(NSCoder *)decoder
{
if (self = [super init]) {
// 读取文件的内容
self.no = [decoder decodeObjectForKey:@"no"];
self.age = [decoder decodeIntForKey:@"age"];
self.height = [decoder decodeDoubleForKey:@"height"];
}
return self;
}
@end
<EMBED id=ZeroClipboardMovie_2 height=18 name=ZeroClipboardMovie_2 type=application/x-shockwave-flash align=middle pluginspage=http://www.macromedia.com/go/getflashplayer width=18 src=http://static.blog.csdn.net/scripts/ZeroClipboard/ZeroClipboard.swf wmode="transparent" flashvars="id=2&width=18&height=18" allowfullscreen="false" allowscriptaccess="always" bgcolor="#ffffff" quality="best" menu="false" loop="false">
[objc]
#import <Foundation/Foundation.h>
@interface MJStudent : NSObject <NSCoding>
@property (nonatomic, copy) NSString *no;
@property (nonatomic, assign) double height;
@property (nonatomic, assign) int age;
@end
<EMBED id=ZeroClipboardMovie_1 height=18 name=ZeroClipboardMovie_1 type=application/x-shockwave-flash align=middle pluginspage=http://www.macromedia.com/go/getflashplayer width=18 src=http://static.blog.csdn.net/scripts/ZeroClipboard/ZeroClipboard.swf wmode="transparent" flashvars="id=1&width=18&height=18" allowfullscreen="false" allowscriptaccess="always" bgcolor="#ffffff" quality="best" menu="false" loop="false">