Objective-C在沙盒中读写plist文件

使用Xcode 9.2 开发语言:Objective-C

测试一:

1)创建一个工程。

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    //获取沙盒目录
    NSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    
    NSString *plistPath1 = [path objectAtIndex:0];
    
    //拼接得到完整的文件名
    NSString *filename = [plistPath1 stringByAppendingPathComponent:@"my.plist"];
    
    //创建数据字典
    NSDictionary *dic = @{@"myname":@"litao"};
    //将数据写入到文件中
    [dic writeToFile:filename atomically:YES];
    
    //设置读取plist 文件目录及名字
    NSDictionary *getDic = [NSDictionary dictionaryWithContentsOfFile:filename];
    //输出结果
    NSLog(@"%@", getDic);
    
    
    // Do any additional setup after loading the view, typically from a nib.
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end

2)输出结果:

2018-03-21 22:35:09.758175+0800 TOCPlistb[2293:104522] {

    myname = litao;

}


测试二) 写入多条记录

1)创建多条记录

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    //获取沙盒目录
    NSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    
    NSString *plistPath1 = [path objectAtIndex:0];
    
    //拼接得到完整的文件名
    NSString *filename = [plistPath1 stringByAppendingPathComponent:@"my.plist"];
    
    //创建数据字典
    NSDictionary *dic = @{@"myname":@"litao",
                          @"myhome":@"Shanghai",
                          @"myage": @30,
                          };
    //将数据写入到文件中
    [dic writeToFile:filename atomically:YES];
    
    //设置读取plist 文件目录及名字
    NSDictionary *getDic = [NSDictionary dictionaryWithContentsOfFile:filename];
    //输出结果
    NSLog(@"%@", getDic);
    
    
    // Do any additional setup after loading the view, typically from a nib.
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end

2)log 输出

2018-03-21 22:39:56.958633+0800 TOCPlistb[2591:120874] {

    myage = 30;

    myhome = Shanghai;

    myname = litao;

}

测试三)

1)输出所有的log

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];

    //获取沙盒目录
    NSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    
    NSLog(@"The path is: %@", path);
    
    NSString *plistPath1 = [path objectAtIndex:0];
    
    NSLog(@"The plistPath1 is : %@", plistPath1);
    
    //拼接得到完整的文件名
    NSString *filename = [plistPath1 stringByAppendingPathComponent:@"my.plist"];
    
    NSLog(@"The file name is %@",filename);
    //创建数据字典
    NSDictionary *dic = @{@"myname":@"litao",
                          @"myhome":@"Shanghai",
                          @"myage": @30,
                          };
    NSLog(@"The created dic is %@", dic);
    
    //将数据写入到文件中
    [dic writeToFile:filename atomically:YES];
    
    //设置读取plist 文件目录及名字
    NSDictionary *getDic = [NSDictionary dictionaryWithContentsOfFile:filename];
    
    //输出结果
    NSLog(@"%@", getDic);
    
    
    // Do any additional setup after loading the view, typically from a nib.
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end


2) 打印log

2018-03-21 22:43:58.389477+0800 TOCPlistb[2835:134289] The path is: (

    "/Users/xxxx/Library/Developer/CoreSimulator/Devices/39B68xxx-xxxx-xxx-xxxx-xxx5D1FE8D5E/data/Containers/Data/Application/C4766DE7-xxxx-xxxx-xxxx-31F33B87xxxx/Documents"

)

2018-03-21 22:43:58.389721+0800 TOCPlistb[2835:134289] The plistPath1 is : /Users/xxxx/Library/Developer/CoreSimulator/Devices/39B68ED5-xxxx-xxxx-xxxx-7E75D1FE8D5E/data/Containers/Data/Application/C476xxxx-xxxx-xxxx-xxxx-3xxxxB872111/Documents

2018-03-21 22:43:58.389886+0800 TOCPlistb[2835:134289] The file name is /Users/xxxx/Library/Developer/CoreSimulator/Devices/39B68ED5-2ED6-4691-AF8D-7E75D1FE8D5E/data/Containers/Data/Application/C476xxxx-xxxx-xxxx-xxxx-xxxx3B872111/Documents/my.plist

2018-03-21 22:43:58.390301+0800 TOCPlistb[2835:134289] The created dic is {

    myage = 30;

    myhome = Shanghai;

    myname = litao;

}

2018-03-21 22:43:58.391567+0800 TOCPlistb[2835:134289] {

    myage = 30;

    myhome = Shanghai;

    myname = litao;

}




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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值