08獲取沙盒路徑_NSUserDefaults_WriteToFile_雙指針

大綱

一、獲取沙盒路徑
尋找沙盒路徑:

NSString *homeDirectoryPath = NSHomeDirectory();

獲取Documents的方法:
1.拼接路徑
2.搜索
二、NSUserDefaults
NSUserDefaults(單例類(lèi)):直接操作 沙盒根目錄下的Library/preference/plist文件
可存儲(chǔ)少量數(shù)據(jù):數(shù)組、字典、字符串、基本數(shù)據(jù)類(lèi)型、NSData
NSUserDefaults是根據(jù)時(shí)間戳將數(shù)據(jù)寫(xiě)入plist文件中,也就是調(diào)用setObject方法時(shí),數(shù)據(jù)不會(huì)立刻寫(xiě)入;如果在此過(guò)程中,程序突然崩潰,會(huì)造成數(shù)據(jù)寫(xiě)入失敗,為避免這種情況出現(xiàn),就同步寫(xiě)入。
synchronize:同步寫(xiě)入(調(diào)用setObject方法時(shí))
三、WriteToFile

writeToFile:

可存儲(chǔ)少量數(shù)據(jù)
特點(diǎn):覆蓋寫(xiě)入
可以存儲(chǔ):數(shù)組/字典/字符串/NSData

File:文件寫(xiě)入路徑
atomically:若路徑下不存在該文件,是否自動(dòng)(創(chuàng)建)

[dict writeToFile:filePath atomically:YES];

error:表示系統(tǒng)返回的文件寫(xiě)入失敗的錯(cuò)誤信息
&error:作為一個(gè)指針傳給系統(tǒng)

NSError *error = nil;
[string writeToFile:filePath atomically:YES encoding:NSUTF8StringEncoding error:&error];

四、雙指針
指針:一種數(shù)據(jù)類(lèi)型
與其他數(shù)據(jù)類(lèi)型的區(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:是否展開(kāi)全路徑
    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(單例類(lèi)):直接操作 沙盒根目錄下Library文件夾下的preference中的plist文件
可以存儲(chǔ)少量數(shù)據(jù):數(shù)組、字典、字符串、基本數(shù)據(jù)類(lèi)型、NSData
NSUserDefaults是根據(jù)時(shí)間戳將數(shù)據(jù)寫(xiě)入plist文件中,也就是調(diào)用setObject方法時(shí),數(shù)據(jù)不會(huì)立刻寫(xiě)入;如果在此過(guò)程中,程序突然崩潰,會(huì)造成數(shù)據(jù)寫(xiě)入失敗,為避免這種情況出現(xiàn),就同步寫(xiě)入。
synchronize:同步寫(xiě)入(調(diào)用setObject方法時(shí))

項(xiàng)目:DataStore_NSUserDefaults0411

    //NSUserDefaults(單例類(lèi)):直接操作 沙盒根目錄下Library文件夾下的preference中的plist文件
    //可以存儲(chǔ)少量數(shù)據(jù):數(shù)組、字典、字符串、基本數(shù)據(jù)類(lèi)型、NSData
    NSString *libraryPath = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES)[0];
#pragma mark - **************** 存儲(chǔ)數(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];
        //寫(xiě)入本地沙盒的plist
        [userDefaults setObject:array forKey:@"name"];
    }
    else if (sender.tag == 1)
    {
        //字符串
        NSString *string = @"周一";
        [userDefaults setObject:string forKey:@"日期"];
    }
    else if (sender.tag == 2)
    {
        //基本數(shù)據(jù)類(lèi)型
        [userDefaults setInteger:28 forKey:@"年齡"];
    }
    else
    {
        //NSUTF8StringEncoding:國(guó)際編碼方式
        NSString *string = @"12345678";
        NSData *data = [string dataUsingEncoding:NSUTF8StringEncoding];
        [userDefaults setObject:data forKey:@"password"];
    }
    //NSUserDefaults是根據(jù)時(shí)間戳將數(shù)據(jù)寫(xiě)入plist文件中,也就是調(diào)用setObject方法時(shí),數(shù)據(jù)不會(huì)立刻寫(xiě)入;如果在此過(guò)程中,程序突然崩潰,會(huì)造成數(shù)據(jù)寫(xiě)入失敗,為避免這種情況出現(xiàn),就同步寫(xiě)入。
    //synchronize:同步寫(xiě)入,調(diào)用setObject方法時(shí),
    [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ù)類(lèi)型
        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:
可存儲(chǔ)少量數(shù)據(jù)
特點(diǎn):覆蓋寫(xiě)入
可以存儲(chǔ):數(shù)組/字典/字符串/NSData

File:文件寫(xiě)入路徑
atomically:若路徑下不存在該文件,是否自動(dòng)(創(chuàng)建)

[dict writeToFile:filePath atomically:YES];

error:表示系統(tǒng)返回的文件寫(xiě)入失敗的錯(cuò)誤信息
&error:作為一個(gè)指針傳給系統(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:
 寫(xiě)文件:存儲(chǔ)少量數(shù)據(jù)
 特點(diǎn):覆蓋寫(xiě)入
 可以存儲(chǔ):數(shù)組/字典/字符串/NSData
 */
#pragma mark - **************** 存儲(chǔ)數(shù)據(jù)
- (IBAction)saveDataClick:(UIButton *)sender
{
    //獲取文件路徑
    NSString *filePath = [self getFilePath];
    if (sender.tag == 0)
    {
        NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:@"張三",@"name", nil];
        //參數(shù)1:將文件寫(xiě)入哪個(gè)路徑下
        //參數(shù)2:是否自動(dòng)(創(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)返回的文件寫(xiě)入失敗的錯(cuò)誤信息
        //&error:作為一個(gè)指針傳給系統(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ù)類(lèi)型
與其他數(shù)據(jù)類(lèi)型的區(qū)別:保存地址
雖然很小,但它也有自己的內(nèi)存地址。

雙指針,指針的指針
作用:通常用于在方法內(nèi)部改變指針指向

項(xiàng)目:DoublePointer0411

- (void)viewDidLoad {
    [super viewDidLoad];
    //指針:一種數(shù)據(jù)類(lèi)型
    //與其他數(shù)據(jù)類(lèi)型的區(qū)別:保存地址
    //雖然很小,但它也有自己的內(nèi)存地址。
    People *people = [[People alloc]init];
    NSLog(@"people%@,people%p,&people%p",people,people,&people);
    
    #pragma mark - **************** 雙指針
    //雙指針:指針的指針
    //people1 和 p 是兩個(gè)不同的指針,修改p不會(huì)影響people1
    //作用:通常用于在方法內(nèi)部改變指針指向
    People *people1 = nil;
    NSLog(@"1.%@,%p,%p",people1,people1,&people1);
    [self setPeople:people1];
    NSLog(@"4.%@,%p,%p",people1,people1,&people1);
    
    //使用雙指針?lè)椒?    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
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • *面試心聲:其實(shí)這些題本人都沒(méi)怎么背,但是在上海 兩周半 面了大約10家 收到差不多3個(gè)offer,總結(jié)起來(lái)就是把...
    Dove_iOS閱讀 27,622評(píng)論 30 472
  • 27、ViewController的didReceiveMemoryWarning是在什么時(shí)候調(diào)用的?默認(rèn)的操作是...
    煙雨平生花飛舞閱讀 693評(píng)論 0 1
  • iOS 開(kāi)発の結(jié)構(gòu) 畫(huà)面 UI UIWebview [[UIApplication sharedApplicati...
    RencaiXiong閱讀 669評(píng)論 0 0
  • iOS本地緩存數(shù)據(jù)方式有五種: 1.直接寫(xiě)文件方式:可以存儲(chǔ)的對(duì)象有NSString、NSArray、NSDict...
    愛(ài)哼的阿貍閱讀 627評(píng)論 0 0
  • 前言 iOS本地緩存數(shù)據(jù)方式有五種: 1.直接寫(xiě)文件方式:可以存儲(chǔ)的對(duì)象有NSString、NSArray、NSD...
    一等到天幻閱讀 2,056評(píng)論 0 1

友情鏈接更多精彩內(nèi)容