数据写到本地

自己总结的。把数据写到本地分4步:

1:写的东西

2:写进哪里

3:写进去

4:写完拿出来




  //获取document文件路径
 NSString *documentStr = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];


//数组写入本地

1:写的东西

NSArray *arr = @[@"1",@"2",@"3"];

2:写进哪里

NSString *srt = [documenStr stringByAppendingPathComponent:@"arr.avi"];


3:写进去

[arr writeToFile: str atomically:YES];

4:写完拿出来

NSArray *array = NSArray arrayWithContentsOfFile:srt];

NSLog(@"%@",array);



//字典写入本地

//直接写入本地的时候 第一次写入之。如果再次对同一个文件进行写入操作,会覆盖之前的。

//一次只能写入一条数据

//如果不想覆盖,就先读出来,然后再一起写进去

//所有能直接写入本地的 一定要遵守NSCoding协议

NSDictonary *dic = @{@"张三":@"李四":@"王麻子"}

NSString *str = [documentStr stringByAppendingString:@"/ss.tee "];

[dic writeToFile:str atomically:YES];

NSDictionary *dic = [nsdictionary dictionaryWithContentsOfFile:str];

NSL(@"document====%@",documentStr);


//图片写入本地

NSString *str = @“http://img.fs0757.com/news/2015/0901//2015090110400752.jpg”;

NSURL *url = [NSURL URLWithString:str];

NSData *data = [NSData dataWithContentsOfURL:url];

    NSString *path = [documentStr stringByAppendingString:@"/猴子.png"];
    [data writeToFile:pathh atomically:YES];
    UIImageView *img = [[UIImageView alloc]initWithFrame:CGRectMake(100, 100, 100, 100)];
    NSData *data1 = [NSData dataWithContentsOfFile:path];
    UIImage *image = [UIImage imageWithData:data1];
    img.image = image;
    [self.view addSubview:img];
    NSLog(@"path == =%@",path);



    //使用的时候 只有写一些简单的数据列如 版本号 是否第一次启动
    NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
 
    //写入数据
    [user setObject:@"张三" forKey:@"0"];
    [user setObject:@"李四"  forKey:@"1"];
    //读取数据
    NSString *string = [user objectForKey:@"1"];
    
    NSLog(@"uesr=====%@",string);



     自己学习总结的一些知识点。



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值