iOS本地存储 plist文件和归档

不管是写入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");
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值