也可以直接創建plist表,對表進行操作,增添數據。這裡使用代碼創建
plist表裡面的對象:
NSString,NSArray,NSDictionary,NSNumber,NSData,NSDate,可修改類的對象,嵌套對象
寫入plist表的方法:writeToFile: atomically:或者writeToURL: atomically:
(1)創建一個student類:
[objc]
@property (copy,nonatomic) NSString *studentName;
@property (assign,nonatomic) int studentID;
@property (copy,nonatomic) NSString *studentSex;
@property (copy,nonatomic) NSString *studentImagePath;//照片的文件路徑
-(id)initWithName:(NSString *)name ID:(int)studentID Sex:(NSString *)sex Photo:(NSString *)imagePath;
-(NSString *)description;
[objc]
-(id)initWithName:(NSString *)name ID:(int)studentID Sex:(NSString *)sex Photo:(NSString *)imagePath {
if ((self = [super init])) {
self.studentName = name;
self.studentID = studentID;
self.studentSex = sex;
self.studentImagePath = imagePath;
}
return self;
}
-(NSString *)description {
return [NSString stringWithFormat:@"name:%@,id:%d,sex:%@,imagepath:%@",self.studentName,self.studentID,self.studentSex,self.studentImagePath];
}
-(void)dealloc {
self.studentName = nil;
self.studentSex = nil;
self.studentImagePath = nil;
}
(2)
[objc]
//創建初始學生數組
-(NSArray *)createStudentArray {
Students *seal = [[Students alloc] initWithName:@"seal" ID:110401 Sex:@"girl" Photo:@"seal.png"];
Students *willing = [[Students alloc] initWithName:@"willing" ID:110402 Sex:@"boy" Photo:@"willing.png"];
Students *lisa = [[Students alloc] initWithName:@"lisa" ID:110403 Sex:@"girl" Photo:@"lisa.png"];
NSArray *studentArray = @[seal,willing,lisa];
return studentArray;
}
[objc]
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
NSLog(@"AppHome:\n%s\n",[NSHomeDirectory() UTF8String]);//獲取應用程序Home目錄的全路徑,打印出來,復制路徑到《前往文件夾》去看看
//指定屬性列表文件路徑
//Documents目錄用於保存程序中的文件,該目錄主要保存應用程序在啟動時加載的文件
//獲取程序的Documents(文件)目錄(Directory)路徑(path)-Home目錄中搜索Documents目錄並返回其全路徑
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);//DocumentDirectory表示搜索的是Documents目錄,NSUserDomainMask,表示搜索范圍緊在應用程序沙盒(Home目錄)內,最後一個bool值表示是否將波浪號轉換為實際路徑。滿足條件的路徑會被放在這個數組裡面。
NSString *documentsDirPath = [paths objectAtIndex:0];//獲取文件目錄的路徑。由於iOS應用程序只會有一個Documents目錄,所有直接返回數組的第一個成員就可以了
NSString *studentFilePath = [documentsDirPath stringByAppendingPathComponent:@"student.plist"];
NSFileManager *fileManager = [NSFileManager defaultManager];//獲取NSFileManager單件對象,用於操作文件。(iOS應用程序通過文件管理器對象對文件系統進行各種高層操作)
BOOL isDirectory; //判斷AppHome/Documents/student.plist文件是否存在(txt純文本)
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];
// 這一種辦法寫入不成功,因為並不是所有對象都可以保存到plist裡面的。 (NSString,NSArray,NSDictionary,NSNumber,NSData,NSDate,可修改類的對象,嵌套對象)
// NSArray *seal = [studentArray objectAtIndex:0];
// NSArray *willing = [studentArray objectAtIndex:1];
// NSArray *lisa = [studentArray objectAtIndex:2];
// NSArray *studentList = @[seal,willing,lisa];
//
//
// NSLog(@"studentList:\n%s\n",[[studentList description] UTF8String]);
if ([studentList writeToFile:studentFilePath atomically:YES]) {
NSLog(@"將student數組保存為屬性列表文件成功!!");
}else{
NSLog(@"將student數組保存為屬性列表文件不成功");
}
}
}
前往文件夾裡面去看:
--