使用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;
}