也可以直接创建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 *)易做图 Photo:(NSString *)imagePath;
-(NSString *)description;
[objc]
-(id)initWithName:(NSString *)name ID:(int)studentID Sex:(NSString *)易做图 Photo:(NSString *)imagePath {
if ((self = [super init])) {
self.studentName = name;
self.studentID = studentID;
self.studentSex = 易做图;
self.studentImagePath = imagePath;
}
return self;
}
-(NSString *)description {
return [NSString stringWithFormat:@"name:%@,id:%d,易做图:%@,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{ &nbs
补充:移动开发 , IOS ,