08獲取沙盒路徑_NSUserDefaults_WriteToFile_雙指針

大綱

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

NSString *homeDirectoryPath = NSHomeDirectory();

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

writeToFile:

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

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

[dict writeToFile:filePath atomically:YES];

error:表示系統(tǒng)返回的文件寫入失敗的錯(cuò)誤信息
&error:作為一個(gè)指針傳給系統(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文件
可以存儲(chǔ)少量數(shù)據(jù):數(shù)組、字典、字符串、基本數(shù)據(jù)類型、NSData
NSUserDefaults是根據(jù)時(shí)間戳將數(shù)據(jù)寫入plist文件中,也就是調(diào)用setObject方法時(shí),數(shù)據(jù)不會(huì)立刻寫入;如果在此過程中,程序突然崩潰,會(huì)造成數(shù)據(jù)寫入失敗,為避免這種情況出現(xiàn),就同步寫入。
synchronize:同步寫入(調(diào)用setObject方法時(shí))

項(xiàng)目:DataStore_NSUserDefaults0411

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

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

[dict writeToFile:filePath atomically:YES];

error:表示系統(tǒng)返回的文件寫入失敗的錯(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:
 寫文件:存儲(chǔ)少量數(shù)據(jù)
 特點(diǎn):覆蓋寫入
 可以存儲(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:將文件寫入哪個(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)返回的文件寫入失敗的錯(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ù)類型
與其他數(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 是兩個(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);
    
    //使用雙指針方法
    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)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 228,646評(píng)論 6 533
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 98,595評(píng)論 3 418
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事。” “怎么了?”我有些...
    開封第一講書人閱讀 176,560評(píng)論 0 376
  • 文/不壞的土叔 我叫張陵,是天一觀的道長。 經(jīng)常有香客問我,道長,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 63,035評(píng)論 1 314
  • 正文 為了忘掉前任,我火速辦了婚禮,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 71,814評(píng)論 6 410
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 55,224評(píng)論 1 324
  • 那天,我揣著相機(jī)與錄音,去河邊找鬼。 笑死,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 43,301評(píng)論 3 442
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢(mèng)啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 42,444評(píng)論 0 288
  • 序言:老撾萬榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 48,988評(píng)論 1 335
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 40,804評(píng)論 3 355
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 42,998評(píng)論 1 370
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 38,544評(píng)論 5 360
  • 正文 年R本政府宣布,位于F島的核電站,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 44,237評(píng)論 3 347
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 34,665評(píng)論 0 26
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 35,927評(píng)論 1 287
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個(gè)月前我還...
    沈念sama閱讀 51,706評(píng)論 3 393
  • 正文 我出身青樓,卻偏偏與公主長得像,于是被迫代替她去往敵國和親。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 47,993評(píng)論 2 374

推薦閱讀更多精彩內(nèi)容

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