不管是写入plist文件还是归档 都会覆盖之前文件里的所有内容 而不是添加
- (void)viewDidLoad {
[super viewDidLoad];
UIButton * btn = [[UIButton alloc]initWithFrame:CGRectMake(50, 100, 50, 50)];
[btn setBackgroundColor:[UIColor redColor]];
[btn addTarget:self action:@selector(qqq) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn];
UIButton * btn2 = [[UIButton alloc]initWithFrame:CGRectMake(50, 200, 50, 50)];
[btn2 setBackgroundColor:[UIColor purpleColor]];
[btn2 addTarget:self action:@selector(wwww) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn2];
UIButton * btn3 = [[UIButton alloc]initWithFrame:CGRectMake(50, 300, 50, 50)];
[btn3 setBackgroundColor:[UIColor orangeColor]];
[btn3 addTarget:self action:@selector(eee) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn3];
//-------------------归档和解档-------------------
UIButton * btn4 = [[UIButton alloc]initWithFrame:CGRectMake(250, 100, 50, 50)];
[btn4 setBackgroundColor:[UIColor redColor]];
[btn4 addTarget:self action:@selector(rrr) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn4];
UIButton * btn5 = [[UIButton alloc]initWithFrame:CGRectMake(250, 200, 50, 50)];
[btn5 setBackgroundColor:[UIColor purpleColor]];
[btn5 addTarget:self action:@selector(ttt) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn5];
UIButton * btn6 = [[UIButton alloc]initWithFrame:CGRectMake(250, 300, 50, 50)];
[btn6 setBackgroundColor:[UIColor orangeColor]];
[btn6 addTarget:self action:@selector(yyy) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:btn6];
}
//创建并写入plist文件
- (void)qqq
{
NSArray * arr = [NSArray arrayWithObjects:@"早乙女露依",@"桃谷绘里香",@"水莱杉", nil];
NSString * path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];
NSString * filePath = [path stringByAppendingString:@"/chichi.plist"];
BOOL success = [arr writeToFile:filePath atomically:YES];
if (success) {
NSLog(@"success");
}else{
NSLog(@"error");
}
}
//读取plist文件内容
- (void)wwww
{
NSString * path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];
NSString * filePath = [path stringByAppendingString:@"/chichi.plist"];
NSArray * arr = [NSArray arrayWithContentsOfFile:filePath];
NSLog(@"%@",arr);
}
//修改plist文件内容
- (void)eee
{
NSArray * arr = [NSArray arrayWithObjects:@"卫紫菱", nil];
NSString * path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];
NSString * filePath = [path stringByAppendingString:@"/chichi.plist"];
BOOL success = [arr writeToFile:filePath atomically:YES];
if (success) {
NSLog(@"success");
}else{
NSLog(@"error");
}
}
//归档
- (void)rrr
{
NSArray * arr = [NSArray arrayWithObjects:@"早乙女露依",@"桃谷绘里香",@"水莱杉", nil];
NSString * path = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)[0];
NSString * filePath = [path stringByAppendingString:@"/骚马.data"];
BOOL succeed = [NSKeyedArchiver archiveRootObject:arr toFile:filePath];
if (succeed) {
NSLog(@"success");
}else{
NSLog(@"error");
}
}
//解档
- (void)ttt
{
NSString * path = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)[0];
NSString * filePath = [path stringByAppendingString:@"/骚马.data"];
NSArray * arr = [NSKeyedUnarchiver unarchiveObjectWithFile:filePath];
NSLog(@"%@",arr);
}
//修改
- (void)yyy
{
NSArray * arr = [NSArray arrayWithObjects:@"古月轩", nil];
NSString * path = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)[0];
NSString * filePath = [path stringByAppendingString:@"/骚马.data"];
BOOL succeed = [NSKeyedArchiver archiveRootObject:arr toFile:filePath];
if (succeed) {
NSLog(@"success");
}else{
NSLog(@"error");
}
}