plist:只适用于系统自带的一些常用类型才能用,且必须先获取路径相对麻烦
NSUserDefaults:将所有的东西都保存在同一个文件夹下面,且主要用于存储应用的设置信息
归档:前两者只能存储常用的类型,而归档可以实现把自定义的对象存放在文件中
序列化是把对象转化成字节序列的过程,反序列化是把字节序列恢复成对象
- (void)encodeWithCoder:(NSCoder *)aCoder{
[aCoder encodeObject:self.nameforKey:@"name"];
[aCoder encodeInt:self.ageforKey:@"age"];
}
- (instancetype)initWithCoder:(NSCoder *)aDecoder{
_name = [aDecoder decodeObjectForKey:@"name"];
_age = [aDecoder decodeIntegerForKey:@"age"];
return self;
}
SLStudent *s = [[SLStudentalloc]init];
s.name = @"shile";
s.age = 25;
NSString *document = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
NSString *path = [document stringByAppendingString:@"person.archiver"];
[NSKeyedArchiverarchiveRootObject:s toFile:path];
NSString *document = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
NSString *path = [document stringByAppendingString:@"person.archiver"];
SLStudent *sss = [NSKeyedUnarchiverunarchiveObjectWithFile:path];
https://github.com/shileseal/SLNSKeyedArchiver