大綱
一崎苗、獲取沙盒路徑
尋找沙盒路徑:
NSString *homeDirectoryPath = NSHomeDirectory();
獲取Documents的方法:
1.拼接路徑
2.搜索
二狐粱、NSUserDefaults
NSUserDefaults(單例類):直接操作 沙盒根目錄下的Library/preference/plist文件
可存儲少量數(shù)據(jù):數(shù)組、字典胆数、字符串肌蜻、基本數(shù)據(jù)類型、NSData
NSUserDefaults是根據(jù)時間戳將數(shù)據(jù)寫入plist文件中必尼,也就是調(diào)用setObject方法時蒋搜,數(shù)據(jù)不會立刻寫入;如果在此過程中判莉,程序突然崩潰豆挽,會造成數(shù)據(jù)寫入失敗,為避免這種情況出現(xiàn)券盅,就同步寫入祷杈。
synchronize:同步寫入(調(diào)用setObject方法時)
三、WriteToFile
writeToFile:
可存儲少量數(shù)據(jù)
特點(diǎn):覆蓋寫入
可以存儲:數(shù)組/字典/字符串/NSData
File:文件寫入路徑
atomically:若路徑下不存在該文件渗饮,是否自動(創(chuàng)建)
[dict writeToFile:filePath atomically:YES];
error:表示系統(tǒng)返回的文件寫入失敗的錯誤信息
&error:作為一個指針傳給系統(tǒng)
NSError *error = nil;
[string writeToFile:filePath atomically:YES encoding:NSUTF8StringEncoding error:&error];
四但汞、雙指針
指針:一種數(shù)據(jù)類型
與其他數(shù)據(jù)類型的區(qū)別:保存地址
雖然很小,但它也有自己的內(nèi)存地址互站。
雙指針私蕾,指針的指針
作用:通常用于在方法內(nèi)部改變指針指向
正文
一、獲取沙盒路徑
尋找沙盒路徑:NSString *homeDirectoryPath = NSHomeDirectory();
獲取Documents的方法:
1.拼接路徑
2.搜索
項(xiàng)目:DataStore0411
尋找沙盒路徑:
- (void)viewDidLoad
{
[super viewDidLoad];
//獲取沙盒路徑
NSString *homeDirectoryPath = NSHomeDirectory();
NSLog(@"%@",homeDirectoryPath);
}
獲取Documents的方法:
//1.拼接路徑
//方法1:
NSString *documentsPath = [NSString stringWithFormat:@"%@/Documents",homeDirectoryPath];
NSLog(@"documentsPath = %@",documentsPath);
//方法2:
NSString *documentsPath2 = [homeDirectoryPath stringByAppendingPathComponent:@"Documents"];
NSLog(@"documentsPath2 = %@",documentsPath2);
//2.搜索
//NSSearchPathDirectory directory:要搜索的文件夾
//NSSearchPathDomainMask domainMask:搜索的區(qū)域
//BOOL expandTilde:是否展開全路徑
NSArray *pathArr = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsPath3 = [pathArr objectAtIndex:0];
NSLog(@"documentsPath3 = %@",documentsPath3);
#pragma mark - **************** Library
NSString *libraryPath1 = [homeDirectoryPath stringByAppendingPathExtension:@"Library"];
NSLog(@"libraryPath1 = %@",libraryPath1);
NSString *libraryPath2 = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES)[0];
NSLog(@"libraryPath2 = %@",libraryPath2);
#pragma mark - **************** tmp
NSString *tmpPath = NSTemporaryDirectory();
NSLog(@"tmpPath = %@",tmpPath);
#pragma mark - **************** app
NSString *appPath = [[NSBundle mainBundle] bundlePath];
NSLog(@"appPath = %@",appPath);
#pragma mark - **************** 文件路徑 右鍵→顯示包內(nèi)容
NSString *imgPath = [[NSBundle mainBundle] pathForResource:@"1" ofType:@"png"];
二胡桃、NSUserDefaults
NSUserDefaults(單例類):直接操作 沙盒根目錄下Library文件夾下的preference中的plist文件
可以存儲少量數(shù)據(jù):數(shù)組踩叭、字典、字符串、基本數(shù)據(jù)類型容贝、NSData
NSUserDefaults是根據(jù)時間戳將數(shù)據(jù)寫入plist文件中自脯,也就是調(diào)用setObject方法時,數(shù)據(jù)不會立刻寫入斤富;如果在此過程中膏潮,程序突然崩潰,會造成數(shù)據(jù)寫入失敗满力,為避免這種情況出現(xiàn)焕参,就同步寫入。
synchronize:同步寫入(調(diào)用setObject方法時)
項(xiàng)目:DataStore_NSUserDefaults0411
//NSUserDefaults(單例類):直接操作 沙盒根目錄下Library文件夾下的preference中的plist文件
//可以存儲少量數(shù)據(jù):數(shù)組油额、字典叠纷、字符串、基本數(shù)據(jù)類型潦嘶、NSData
NSString *libraryPath = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES)[0];
#pragma mark - **************** 存儲數(shù)據(jù)
//[userDefaults setObject:array forKey:@"name"];
//NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
- (IBAction)saveDataClick:(UIButton *)sender
{
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
if (sender.tag == 0)
{
//數(shù)組/字典
NSArray *array = [[NSArray alloc]initWithObjects:@"張三",@"李四", nil];
//寫入本地沙盒的plist
[userDefaults setObject:array forKey:@"name"];
}
else if (sender.tag == 1)
{
//字符串
NSString *string = @"周一";
[userDefaults setObject:string forKey:@"日期"];
}
else if (sender.tag == 2)
{
//基本數(shù)據(jù)類型
[userDefaults setInteger:28 forKey:@"年齡"];
}
else
{
//NSUTF8StringEncoding:國際編碼方式
NSString *string = @"12345678";
NSData *data = [string dataUsingEncoding:NSUTF8StringEncoding];
[userDefaults setObject:data forKey:@"password"];
}
//NSUserDefaults是根據(jù)時間戳將數(shù)據(jù)寫入plist文件中涩嚣,也就是調(diào)用setObject方法時,數(shù)據(jù)不會立刻寫入掂僵;如果在此過程中航厚,程序突然崩潰,會造成數(shù)據(jù)寫入失敗看峻,為避免這種情況出現(xiàn),就同步寫入衙吩。
//synchronize:同步寫入互妓,調(diào)用setObject方法時,
[userDefaults synchronize];
}
#pragma mark - **************** 讀取數(shù)據(jù)
- (IBAction)readDataClick:(UIButton *)sender
{
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
if (sender.tag == 0)
{
//數(shù)組/字典
NSArray *array = [userDefaults objectForKey:@"name"];
NSLog(@"%@",array);
}
else if (sender.tag == 1)
{
//字符串
NSString *string = [userDefaults objectForKey:@"日期"];
NSLog(@"%@",string);
}
else if (sender.tag == 2)
{
//基本數(shù)據(jù)類型
NSInteger age = [userDefaults integerForKey:@"年齡"];
NSLog(@"%d",age);
}
else
{
//NSData
NSData *data = [userDefaults objectForKey:@"password"];
NSString *str = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
NSLog(@"%@",str);
}
}
三坤塞、WriteToFile
writeToFile:
可存儲少量數(shù)據(jù)
特點(diǎn):覆蓋寫入
可以存儲:數(shù)組/字典/字符串/NSData
File:文件寫入路徑
atomically:若路徑下不存在該文件冯勉,是否自動(創(chuàng)建)
[dict writeToFile:filePath atomically:YES];
error:表示系統(tǒng)返回的文件寫入失敗的錯誤信息
&error:作為一個指針傳給系統(tǒng)
NSError *error = nil;
[string writeToFile:filePath atomically:YES encoding:NSUTF8StringEncoding error:&error];
項(xiàng)目:DataStore_WriteToFile0411
- (NSString *)getFilePath
{
NSString *documentsPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];
NSLog(@"%@",documentsPath);
NSString *filePath = [documentsPath stringByAppendingPathComponent:@"text.plist"];
return filePath;
}
/**
writeToFile:
寫文件:存儲少量數(shù)據(jù)
特點(diǎn):覆蓋寫入
可以存儲:數(shù)組/字典/字符串/NSData
*/
#pragma mark - **************** 存儲數(shù)據(jù)
- (IBAction)saveDataClick:(UIButton *)sender
{
//獲取文件路徑
NSString *filePath = [self getFilePath];
if (sender.tag == 0)
{
NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:@"張三",@"name", nil];
//參數(shù)1:將文件寫入哪個路徑下
//參數(shù)2:是否自動(創(chuàng)建)該路徑下的文件
[dict writeToFile:filePath atomically:YES];
}
else if (sender.tag == 1)
{
NSString *string = @"111";
[string writeToFile:filePath atomically:YES encoding:NSUTF8StringEncoding error:nil];
//error:表示系統(tǒng)返回的文件寫入失敗的錯誤信息
//&error:作為一個指針傳給系統(tǒng)
// NSError *error = nil;
// [string writeToFile:filePath atomically:YES encoding:NSUTF8StringEncoding error:&error];
}
else
{
NSString *str = @"67人";
NSData *data = [str dataUsingEncoding:NSUTF8StringEncoding];
[data writeToFile:filePath atomically:YES];
[data writeToFile:filePath atomically:YES];
}
}
#pragma mark - **************** 讀取數(shù)據(jù)
- (IBAction)readDataClick:(UIButton *)sender
{
NSString *filePath = [self getFilePath];
if (sender.tag == 0)
{
//方法1:
// NSDictionary *dic = [[NSDictionary alloc]initWithContentsOfFile:filePath];
//方法2:
NSDictionary *dic = [NSDictionary dictionaryWithContentsOfFile:filePath];
NSLog(@"%@",dic);
}
else if (sender.tag == 1)
{
// NSString *string = [[NSString alloc]initWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil];
NSString *string = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil];
NSLog(@"str = %@",string);
}
else
{
// NSData *data = [[NSData alloc]initWithContentsOfFile:filePath];
NSData *data = [NSData dataWithContentsOfFile:filePath];
NSString *str = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
NSLog(@"str = %@",str);
}
}
四、雙指針
指針:一種數(shù)據(jù)類型
與其他數(shù)據(jù)類型的區(qū)別:保存地址
雖然很小摹芙,但它也有自己的內(nèi)存地址灼狰。
雙指針,指針的指針
作用:通常用于在方法內(nèi)部改變指針指向
項(xiàng)目:DoublePointer0411
- (void)viewDidLoad {
[super viewDidLoad];
//指針:一種數(shù)據(jù)類型
//與其他數(shù)據(jù)類型的區(qū)別:保存地址
//雖然很小浮禾,但它也有自己的內(nèi)存地址交胚。
People *people = [[People alloc]init];
NSLog(@"people%@,people%p,&people%p",people,people,&people);
#pragma mark - **************** 雙指針
//雙指針:指針的指針
//people1 和 p 是兩個不同的指針,修改p不會影響people1
//作用:通常用于在方法內(nèi)部改變指針指向
People *people1 = nil;
NSLog(@"1.%@,%p,%p",people1,people1,&people1);
[self setPeople:people1];
NSLog(@"4.%@,%p,%p",people1,people1,&people1);
//使用雙指針方法
People *people2 = nil;
NSLog(@"1.%@,%p,%p",people2,people2,&people2);
[self createPeople:&people2];
}
//雙指針
- (void)createPeople:(People **)p
{
NSLog(@"2.%@,%p,%p",*p,*p,&*p);
NSLog(@"3.(null),%p,%p",p,&p);
//*p:表示獲取people2的指針
*p = [People new];
NSLog(@"4.%@,%p,%p",*p,*p,&*p);
}
//單指針
- (void)setPeople:(People *)p
{
NSLog(@"2.%@,%p,%p",p,p,&p);
p = [People new];
NSLog(@"3.%@,%p,%p",p,p,&p);
}
@end