首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 其他教程 > 操作系统 >

iOS -使用属性列表序列化容易对象-指定序列化与反序列化的编码格式

2013-11-03 
iOS -使用属性列表序列化简单对象-指定序列化与反序列化的编码格式 接上一文:点击打开链接使用属性列表序

iOS -使用属性列表序列化简单对象-指定序列化与反序列化的编码格式

 接上一文:  点击打开链接   使用属性列表序列化简单对象-创建plist表,向plist表添加数据

 指定序列化格式化的编码格式(指定一种编码方式,比如xml编码,二进制编码。这样子:(1)把属性列表对象编码为某种格式的nsdata对象(2)将nsdata对象写入文件或是url。反序列化(加载属性列表)的时候,先读取文件或url到nsdata对象,然后在将其解码为原始的属性列表对象)


指定一种编码,把数据写入plist表

 if ([fileManager fileExistsAtPath:studentFilePath isDirectory:&isDirectory] && !isDirectory) {                NSLog(@"存在 student.plist !!");                 //加载属性列表文件到NSData对象        NSData *studentListData = [NSData dataWithContentsOfFile:studentFilePath];                if (studentListData) {                        //加载为NSData对象成功            NSError *error = nil;             //将NSData对象解码为原始属性列表对象            NSArray *studentList = [NSPropertyListSerialization propertyListWithData:studentListData options:0 format:NULL error:&error];                        if (studentList) {                                 //将NSData对象解码为原始属性列表对象成功                NSArray *studentName = [studentList objectAtIndex:0];                NSArray *studentID = [studentList objectAtIndex:1];                NSArray *studentSex = [studentList objectAtIndex:2];                NSArray *studentImagePath = [studentList objectAtIndex:3];                                NSMutableArray *studentArray = [[NSMutableArray alloc]init];                for (int i = 0; i < [studentName count]; i++) {                    Students *student = [[Students alloc]initWithName:[studentName objectAtIndex:i] ID:[[studentID objectAtIndex:i] intValue] Sex:[studentSex objectAtIndex:i] Photo:[studentImagePath objectAtIndex:i]];                    [studentArray addObject:student];                }                                NSLog(@"students:\n%s\n",[[studentArray description]UTF8String]);            }else {                //解码为原始属性列表对象失败                NSLog(@"解码为原始属性列表对象失败:%s",[[error description] UTF8String]);                        }                  }else{        

打印:

2013-10-28 16:48:24.240 StudentPlist[2387:70b] students:

(

    "name:seal,id:110401,sex:girl,imagepath:seal.png",

    "name:willing,id:110402,sex:boy,imagepath:willing.png",

    "name:lisa,id:110403,sex:girl,imagepath:lisa.png"

)



热点排行