Object-c 归档--源自技术
来源:互联网 发布:什么测温软件准确 编辑:程序博客网 时间:2024/05/21 09:12
一、OBj-C类的归档包含以下:
NString、NSArray、NSDicionary、NSSet、NSDate、NSNumber、NSdata。这还包括嵌套对象,如包含字符串,甚至其他数组对象的数组。
可以使用writeToFile、writeToURL归档
- (BOOL)writeToFile:(NSString *)path atomically:(BOOL)useAuxiliaryFile;
- (BOOL)writeToURL:(NSURL *)url atomically:(BOOL)atomically;
可以使用dictionaryWithContentsOfFile、dictionaryWithContentsOfURL、initWithContentsOfFile、initWithContentsOfURL还原
+ (id /* NSDictionary * */)dictionaryWithContentsOfFile:(NSString *)path;
+ (id /* NSDictionary * */)dictionaryWithContentsOfURL:(NSURL *)url;
- (id /* NSDictionary * */)initWithContentsOfFile:(NSString *)path;
- (id /* NSDictionary * */)initWithContentsOfURL:(NSURL *)url;
上述OBj-C类可以直接使用以下方法进行编码和解码
+ (BOOL)archiveRootObject:(id)rootObject toFile:(NSString *)path;
+ (id)unarchiveObjectWithFile:(NSString *)path;
自定义的类需要实现NSCoding协议的encodeWithCoder、initWithCoder函数,然后使用NSKeyedArchiver进行编码和解码
- -(void) encodeWithCoder:(NSCoder *)aCoder
- {
- [super encodeWithCoder:aCoder];//先执行父类的编码
- [aCoder encodeObject:km forKey:@"UserDetailswl"];
- [aCoder encodeDouble:mark forKey:@"UserDetailsmark"];
- NSLog(@"UserDetails encode-------");
- }
- -(id) initWithCoder:(NSCoder *)aDecoder
- {
- self = [super initWithCoder:aDecoder];<span style="font-family: Menlo; ">//先执行父类的解码</span>
- if (self) {
- km = [aDecoder decodeObjectForKey:@"UserDetailswl"];
- mark = [aDecoder decodeDoubleForKey:@"UserDetailsmark"];
- NSLog(@"UserDetails decoder-------------");
- }
- return self;
- }
三、时用NSData批量归档
- +(void) objEncodeAndDncodeNsData
- {
- UserDetails * details;
- UserDetails * details2;
- NSMutableData * date;
- NSKeyedArchiver * archiver;
- NSString * path;
- details = [[UserDetails alloc] init];
- details.km = @"物理";
- details.mark = 12.6;
- details.name = @"小静静";
- details.age = [[NSNumber alloc] initWithInt:18];
- details2 = [[UserDetails alloc] init];
- details2.km = @"数学";
- details2.mark = 24;
- details2.name = @"那英";
- details2.age = [[NSNumber alloc] initWithInt:18];
- date = [NSMutableData data];
- archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:date];
- [archiver encodeObject:details forKey:@"details"];
- [archiver encodeObject:details2 forKey:@"details2"];
- [archiver finishEncoding];
- path =[[NSString alloc] initWithFormat:@"%@details.archive",NSTemporaryDirectory()];
- if ([date writeToFile:path atomically:YES]) {
- NSLog(@"归档成功!");
- }
- NSLog(@"--------解码-------");
- UserDetails * details_;
- UserDetails * details2_;
- NSMutableData * date_;
- NSKeyedUnarchiver * unArchiver;
- date_ = [NSMutableData dataWithContentsOfFile:path];
- unArchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:date_];
- details_ = [unArchiver decodeObjectForKey:@"details"];
- details2_ = [unArchiver decodeObjectForKey:@"details2"];
- [unArchiver finishDecoding];
- NSLog(@"details_ :%@",details_);
- NSLog(@"details2_ :%@",details2_);
- NSLog(@"details_.km=%@ ,details_.mark=%lf ,details_.name=%@ ,details_.age=%@ ",details_.km,details_.mark,details_.name,details_.age);
- NSLog(@"details2_.km=%@ ,details2_.mark=%lf ,details2_.name=%@ ,details2_.age=%@ ",details2_.km,details2_.mark,details2_.name,details2_.age);
- }
- +(void) objCopy
- {
- NSMutableArray * array;
- NSMutableArray * array_;
- NSData * data;
- array = [[NSMutableArray alloc] initWithObjects:@"a",@"b",@"c", nil nil];
- data = [NSKeyedArchiver archivedDataWithRootObject:array];
- array_ = [NSKeyedUnarchiver unarchiveObjectWithData:data];
- [array addObject:@"d"];
- for (int i=0; i<[array count]; i++) {
- NSLog(@"array %i %@",i+1,[array objectAtIndex:i]);
- }
- for (int i=0; i<[array_ count]; i++) {
- NSLog(@"array_ %i %@",i+1,[array objectAtIndex:i]);
- }
- }
- Object-c 归档--源自技术
- Object-C-归档
- Object-c Archiving归档
- Object-c 归档
- Object-c归档使用
- Object-C 归档、解档
- <c:choose>标签的使用--源自技术
- Object-C 学习笔记(三十)--- 归档(3)---对象归档
- Object-C学习(十):归档
- Object-C中自定义对象的归档
- JAVA AOP--源自技术
- JAVA JDBC--源自技术
- JPA 注解--源自技术
- ActiveMQ简述--源自技术
- 数据库索引--源自技术
- Object-C 基础之10 — 使用xml进行归档
- Object-C 基础之10 — 使用xml进行归档
- Object-C使用XML属性列表进行归档
- 拯救纠结症 选iPhone SE还是iPhone6?
- zoj 1851 Code Formatter 模拟
- 个入侵检测程序来防止恶意刷新的功能
- 第六周【书面项目1】“IT妹子类”的设计UML
- 什么是算法的复杂度?
- Object-c 归档--源自技术
- install pycharm on Ubuntu
- UDID替代方案
- Maven入门(含实例教程)
- LeetCode 24. Swap Nodes in Pairs
- 函数式编程入门思想分享
- Javascript学习之路 —— 函数节流与函数去抖
- javascript级联插件
- 第六周项目三IP地址类