读书人

iOS -使用属性列表序列化容易对象-从

发布时间: 2013-10-29 12:07:57 作者: rapoo

iOS -使用属性列表序列化简单对象-从plist表中读取对象

接上一篇: 点击打开链接

创建了plist表,写入plist表中的数据不是一个student对象,我们要看的时候,需要以一个student对象去看。。。需要从plist表中恢复student对象的信息。我们只需要添加以下代码:

 BOOL isDirectory; //判断AppHome/Documents/student.plist文件是否存在(txt纯文本)    if ([fileManager fileExistsAtPath:studentFilePath isDirectory:&isDirectory] && !isDirectory) {                NSLog(@"存在 student.plist !!");        //        通过plist表生成数组对象        NSArray *studentList = [NSArray arrayWithContentsOfFile:studentFilePath];         //取出数组中对应字段的数组        NSArray *studentName = [studentList objectAtIndex:0];        NSArray *studentID = [studentList objectAtIndex:1];        NSArray *studentSex = [studentList objectAtIndex:2];        NSArray *studentImagePath = [studentList objectAtIndex:3];                 //通过遍历每个字段数组,恢复student对象        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(@"stuents:%s",[[studentArray description] UTF8String]);                    }else{

打印:

2013-10-28 15:31:35.034 StudentPlist[2050:70b] stuents:(

"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"

)



读书人网 >操作系统

热点推荐