保存数据的步骤:
1.新建一个保存数据的路径
2.将数据写入对应的路径
基本函数简介:
①NSString * NSHomeDirectory ( void );
返回值是主目录。在IOS中表示当年打开程序的沙盒的主目录。
②NSArray * NSSearchPathForDirectoriesInDomains ( NSSearchPathDirectory directory, NSSearchPathDomainMask domainMask, BOOL expandTilde );
返回值:是一个字符串数组,保存查找到的当前目录。
第一个参数类型:NSSearchPathDirectory表示要查找的目录。
https://developer.apple.com/library/ios/documentation/Cocoa/Reference/Foundation/Miscellaneous/Foundation_Constants/index.html#//apple_ref/doc/c_ref/NSSearchPathDirectory
第二个参数类型:NSSearchPathDomainMask是一个根目录,用来指定查找范围
enum {
NSUserDomainMask = 1,
NSLocalDomainMask = 2,
NSNetworkDomainMask = 4,
NSSystemDomainMask = 8,
NSAllDomainsMask = 0x0ffff ,
};
typedef NSUInteger NSSearchPathDomainMask;
第三个参数类型:BOOL 表示是否展开目录的波浪号,通常是YES。
③-(NSString *)stringByAppendingPathComponent:(NSString *)aString
该函数用来连接字符串,并且只能作用在文件路径上。
如果文件名为"AyaseEli.plist"添加到相应的路径会有不同的表现
"/tmp/" ----------> /tmp/AyaseEli.plist
"/tmp" ----------> /tmp/AyaseEli.plist
"/" ----------> /AyaseEli.plist
"" ---------> AyaseEli.plist
④-(BOOL)writeToFile:(NSString *)path
atomically:(BOOL)atomically
返回值:YES表示写入成功,否则NO。
第一个参数:(NSString *)path表示要写入的路径,path为路径+文件名。
第二个参数:(BOOL)
atomically YES表示数据写入一个备用的文件。也就是保存要保存的文件改名了,改成path路径中的文件名。否则就保存原始文件名。
实例:将NSDictionary 和 NSArray写入plist
/*
在界面上添加两个按钮
1) 其中一个将NSArray写入文档目录
2) 另一个将NSDictionary写入文档目录
写入文件的步骤:
1. 确定文件路径
2. 将内容写入文件
不过,要写入plist文件,数据类型是有限制的:NSInteger,CGFloat,NSString,NSDate
*/
- (void)viewDidLoad
{
[super viewDidLoad];
UIButton *button1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button1 setFrame:CGRectMake(110, 100, 100, 40)];
[button1 setTitle:@"数组" forState:UIControlStateNormal];
[button1 addTarget:self action:@selector(writeArray) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button1];
UIButton *button2 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[button2 setFrame:CGRectMake(110, 200, 100, 40)];
[button2 setTitle:@"字典" forState:UIControlStateNormal];
[button2 addTarget:self action:@selector(writeDict) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button2];
}
#pragma mark - Actions
- (void)writeArray
{
NSLog(@"写入数组");
// 1. 定义数组
NSArray *array = @[@1, @2, @3, @4];
// 2. 确定要写入的位置
NSArray *documents = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *doc = documents[0];
NSString *path = [doc stringByAppendingPathComponent:@"array.plist"];
NSLog(@"%@", doc);
// 3. 写入数组
[array writeToFile:path atomically:YES];
// 4. 写入一个字符串数组
NSMutableArray *strArray = [NSMutableArray arrayWithCapacity:20];
for (NSInteger i = 0; i < 20; i++) {
NSString *text = [NSString stringWithFormat:@"itcast-%d", arc4random_uniform(10000)];
[strArray addObject:text];
}
NSString *strPath = [doc stringByAppendingPathComponent:@"str.plist"];
[strArray writeToFile:strPath atomically:YES];
}
- (void)writeDict
{
NSLog(@"写入字典");
// 1. 实例化字典
NSMutableDictionary *dict = [NSMutableDictionary dictionary];
for (NSInteger i = 0; i < 20; i++) {
NSString *keyName = [NSString stringWithFormat:@"key-%02d", i];
NSMutableArray *array = [NSMutableArray array];
NSInteger rnd = arc4random_uniform(10) + 5;
for (NSInteger j = 0; j < rnd; j++) {
NSString *text = [NSString stringWithFormat:@"itcast-%d", arc4random_uniform(10000)];
[array addObject:text];
}
[dict setValue:array forKey:keyName];
}
// 2. 写入文件
NSArray *documents = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *path = [documents[0] stringByAppendingPathComponent:@"dict.plist"];
[dict writeToFile:path atomically:YES];
}