把数据保存到本地

Student.h

#import <Foundation/Foundation.h>

@interface Student : NSObject<NSCoding>


@property(nonatomic,copy)NSString *name;
@property(nonatomic,copy)NSString *sex;
@property(nonatomic,assign)NSInteger age;
@property(nonatomic,copy)NSString *hobby;



@end

Student.m

#import "Student.h"

@implementation Student
//有两个必须实现的协议方法,一个是存入到本地的时候用
- (void)encodeWithCoder:(NSCoder *)aCoder{
    //先对这些内容进行编码,然后存储
    [aCoder encodeObject:self.name forKey:@"name"];
    [aCoder encodeObject:self.sex forKey:@"sex"];
    [aCoder encodeObject:self.hobby forKey:@"hobby"];
    [aCoder encodeInteger:self.age forKey:@"age"];


}

//本地读取时用
- (nullable instancetype)initWithCoder:(NSCoder *)aDecoder{
    self=[super init];
    if (self) {
        self.name= [aDecoder decodeObjectForKey:@"name"];
        self.sex=[aDecoder decodeObjectForKey:@"sex"];
        self.hobby=[aDecoder decodeObjectForKey:@"hobby"];
        self.age=[aDecoder decodeIntegerForKey:@"age"];
    }
    return self;

}

-(void)setValue:(id)value forUndefinedKey:(NSString *)key{

}

@end

ViewController.m

#import "ViewController.h"
#import "Student.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    //bundle是获取当前工程的所有文件路径,这个路径是根据当前程序状态来找的,不受影响
//    NSString *path=[[NSBundle mainBundle] pathForResource:@"area" ofType:@"txt"];
//    NSLog(@"%@",path);

//    NSString *pic=[[NSBundle mainBundle] pathForResource:@"c4" ofType:@"jpg"];
//    NSData *data=[NSData dataWithContentsOfFile:pic];
//    [UIImage imageWithData:data];

    //沙盒
    //沙盒路径
//    NSLog(@"%@",NSHomeDirectory());
    //沙盒路径每运行一次,文件名就会变化一次,保证在以后的使用中,不会被入侵,保证存储数据安全性
    //沙盒里有三个文件夹
    //Documents文件夹:用来保存用户信息,比如收藏,或者自己设置的一些信息,所以我们在做收藏功能的时候一般放这个文件夹里
    //Library:一般是保存一些开发者使用的东西,里面包含两个文件夹,,一个是saches,一个是Preferences
    //saches:保存的是工程缓存文件,通过sd加载的图片一般都保存在这个文件夹里
    //Preferences保存像NSUserDefaults这样的数据
    //tep:保存的是临时文件,一般在做图片视频选取的时候,会把文件临时保存在这个文件夹

    //简单对象:字符串,数组,字典等
//    NSString *str=@"后天就放假了!!!开心.....";
//    //1.先获取沙盒路径
//    //参数一:指定前往哪一个文件夹,NSDocumentDirectory前往的是沙盒的Documents文件夹 ,NSCachesDirectory指定前往caches文件夹
//    //参数二:指定文件类型,就是用户类型
//    //参数三:设置是绝对路径还是相对路径
//    NSArray *sandBoxPath=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
//    //YES对应的是绝对路径,根据这个路径可以直接找到路径所对的文件夹,这个是给系统用的,NO对应的是相对路径,是给我们自己看的,通过相对路径可以看到目标文件是哪个
//    
//    NSString *sandBox=sandBoxPath[0];
//    NSLog(@"%@",sandBox);
//    
//    //2.创建一个存储数据的文件,文件需要先拼接路径
//    NSString *docPath=[sandBox stringByAppendingString:@"/放假.xml"];
//    NSLog(@"%@",docPath);
//    
//    //3.写入
//    //参数 1:路径
//    //参数2:是否保护文件
//    //参数3:编码
//    //参数4:nil
//    [str writeToFile:docPath atomically:YES encoding:NSUTF8StringEncoding error:nil];
//    
//    //根据路径把本地的数据读取出来
//    NSString *string=[NSString stringWithContentsOfFile:docPath encoding:NSUTF8StringEncoding error:nil];
//    NSLog(@"%@",string);
//


    //把数组对象写入到本地
//    NSArray *arr=@[@"1",@"2",@"3",@"4",@"5"];
//    //1.找到沙盒的路径
//    NSArray *sandBox=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
//    NSString *sandBoxPath=sandBox[0];
//    //2.拼接文件路径
//    NSString *docPath=[sandBoxPath stringByAppendingPathComponent:@"arr.plist"];
//    //stringByAppendingPathComponent不需要加/
//    NSLog(@"%@",docPath);
//    //3.把数组写入到本地
//    [arr writeToFile:docPath atomically:YES];
//    //把数组从本地读出来
//    NSArray *newArr=[NSArray arrayWithContentsOfFile:docPath];
//    NSLog(@"%@",newArr);


    //字典

//    NSDictionary *dic=@{@"name":@"coco",@"sex":@"m",@"age":@"21"};
//    NSArray *sandBox=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
//    NSString *sandBoxPath=sandBox[0];
//    NSString *docPath=[sandBoxPath stringByAppendingPathComponent:@"dic.txt"];
//    [dic writeToFile:docPath atomically:YES];
//    NSDictionary *newDic=[NSDictionary dictionaryWithContentsOfFile:docPath];
//    NSLog(@"%@",newDic);



//    Student *stu=[[Student alloc] init];
//    stu.name=@"张三";
//    stu.sex=@"男";
//    stu.age=18;
//    stu.hobby=@"玩";
//    //1.
//    NSArray *sandBox=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, 1, YES);
//    NSString *sandBoxPath=sandBox[0];
//    //2.
//    NSString *docPath=[sandBoxPath stringByAppendingPathComponent:@"撒旦.av"];
//    //3.归档
//    [NSKeyedArchiver archiveRootObject:stu toFile:docPath];
//    
//    //4.反归档
//    Student *newStu = [NSKeyedUnarchiver unarchiveObjectWithFile:docPath];
//    NSLog(@"%@",newStu.name);


//    NSArray *arr=@[@"1",@"2",@"3",@"4"];
//    NSArray *sandBox=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, 1, YES);
//    NSString *sandBoxPath=sandBox[0];
//    NSString *docPath=[sandBoxPath stringByAppendingPathComponent:@"arr.txt"];
//    [NSKeyedArchiver archiveRootObject:arr toFile:docPath];
//    NSLog(@"%@",docPath);
//    NSArray *newArr=[NSKeyedUnarchiver unarchiveObjectWithFile:docPath];
//    NSLog(@"%@",newArr);

    //NSArray已经实现了NSCoding协议,所以可以直接用归档和反归档
    //如果数组里是字符串这种简单对象,用writeToFiel和归档都可以,但如果数组里放的是Student这种自定义对象,只能用归档



    Student *stu1=[[Student alloc] init];
    stu1.name=@"张三";
    Student *stu2=[[Student alloc] init];
    stu2.name=@"李四";
    Student *stu3=[[Student alloc] init];
    stu3.name=@"王五";
    NSArray *arr=@[stu1,stu2,stu3];
    NSArray *sandBox=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, 1, YES);
    NSString *sandBoxPath=sandBox[0];
    NSString *docPath=[sandBoxPath stringByAppendingPathComponent:@"student.list"];
    [NSKeyedArchiver archiveRootObject:arr toFile:docPath];
    NSArray *newArr= [NSKeyedUnarchiver unarchiveObjectWithFile:docPath];

    for (Student *stu in newArr) {
        NSLog(@"%@",stu.name);
    }

}

NSUserDefaults

 NSUserDefaults *defaulte=[NSUserDefaults standardUserDefaults];
    [defaulte setObject:@"1" forKey:@"2"];
    NSLog(@"%@",docPath);
    NSLog(@"%@",[defaulte objectForKey:@"2"]);

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值