if ([fileManager fileExistsAtPath:studentFilePath isDirectory:&isDirectory] && !isDirectory) { NSLog(@"存在 student.plist !!"); }else{ NSLog(@"不存在 student.plist"); NSArray *studentArray = [self createStudentArray];//創建student對象 if ([studentArray writeToFile:studentFilePath atomically:YES]) { NSLog(@"將student數組保存為屬性列表文件成功!!"); }else{ NSLog(@"將student數組保存為屬性列表文件不成功"); } // 使用kvc構建字段數組 NSArray *studentIDArray = [studentArray valueForKey:@"studentID"]; NSArray *studentNameArray = [studentArray valueForKey:@"studentName"]; NSArray *studentSexArray = [studentArray valueForKey:@"studentSex"]; NSArray *studentImagePathArray = [studentArray valueForKey:@"studentImagePath"]; NSArray *studentList = @[studentNameArray,studentIDArray,studentSexArray,studentImagePathArray]; // 指定序列化格式化的編碼格式(指定一種編碼方式,比如xml編碼,二進制編碼。這樣子:(1)把屬性列表對象編碼為某種格式的nsdata對象(2)將nsdata對象寫入文件或是url。反序列化(加載屬性列表)的時候,先讀取文件或url到nsdata對象,然後在將其解碼為原始的屬性列表對象) NSError *error = nil; //將屬性列表對象按照指定編碼方式轉換為NSData對象 NSData *studentInfoData = [NSPropertyListSerialization dataWithPropertyList:studentList format:NSPropertyListBinaryFormat_v1_0 options:0 error:&error]; //調用NSPropertyListSerialization類的dataWithPropertyList: format:format options:error:方法將屬性列表對象編碼為指定格式的nsdata對象。NSPropertyListXMLFormat_v1_0是xml編碼/NSPropertyListBinaryFormat_v1_0二進制編碼。 if (studentInfoData) { // 轉換成功 NSLog(@"studentInfoData:\n%s\n",[[studentInfoData description] UTF8String]); //將NSData寫入文件 if ([studentInfoData writeToFile:studentFilePath atomically:YES]) { NSLog(@"將student數組保存為屬性列表文件成功!"); }else{ NSLog(@"將student數組保存為屬性列表文件不成功"); } }else{ NSLog(@"將屬性列表對象按照指定編碼方式轉換為NSData對象失敗:\n%s\n",[[error description]UTF8String]); } }
--------------------- 反序列化的編碼格式:從plist表中讀取student對象
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" )