ios数据存储NSString,NSArray,NSDictionary

    //获取文件路径,路径名不可随意取,不可以光使用文件名作为路径
    NSArray*paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask,YES);
    
    NSString*directory=[paths objectAtIndex:0];
    
    NSString*path=[NSString stringWithFormat:@"%@/data.txt",directory ];
    
    
    
    NSArray* arr=@[@"abc",@"def",@"xxx"];
    [arr writeToFile:path atomically:YES];
    
    NSArray*dats= [NSArray arrayWithContentsOfFile:path];
    NSLog(@"读取Array文件内容 ---- ");

    for (NSString *s in dats) {
       NSLog(@"%@",s);
    }
    
    
    NSDictionary*map=@{@"k1":@"v1",@"k2":@"v2"};
    [map writeToFile:path atomically:YES];
    
    NSDictionary* datamap=[NSDictionary dictionaryWithContentsOfFile:path];
    
    NSLog(@"读取Directory文件内容 ---- ");
    NSLog(@"%@",datamap);
    
    
    [@"abc-str-xxx" writeToFile:path atomically:YES encoding:NSUTF8StringEncoding error:nil];
    NSString*s=[NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:nil];
    NSLog(@"读取NSString文件内容 ---- %@",s);
    

打印如下

2015-10-10 20:57:59.697 table[9594:708660] Unknown class ViewController in Interface Builder file.
2015-10-10 20:57:59.731 table[9594:708660] 读取Array文件内容 ---- 
2015-10-10 20:57:59.731 table[9594:708660] abc
2015-10-10 20:57:59.732 table[9594:708660] def
2015-10-10 20:57:59.732 table[9594:708660] xxx
2015-10-10 20:57:59.735 table[9594:708660] 读取Directory文件内容 ---- 
2015-10-10 20:57:59.736 table[9594:708660] {
    k1 = v1;
    k2 = v2;
}
2015-10-10 20:57:59.739 table[9594:708660] 读取NSString文件内容 ---- abc-str-xxx


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值