sandBox 簡(jiǎn)單對(duì)象的寫入,讀取

//
//  ViewController.m
//  sandBox
//
//  Created by zhaoguodong on 16/6/24.
//  Copyright ? 2016年 zhaoguodong. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    NSString *sandBoxpath = NSHomeDirectory();
    # #注意每個(gè)文件夾的名字,大小寫不能錯(cuò)
    /**
     *  每個(gè)沙盒路徑下都有三個(gè)文件夾
     *Documents 是最常用的,可以儲(chǔ)存一些不太大的數(shù)據(jù),plist、文本、splite。它會(huì)和itunes同步
!
     *Library
          caches:儲(chǔ)存大容量的數(shù)據(jù),caches路徑的文件不會(huì)和itunes同步
          preference:儲(chǔ)存用戶設(shè)置。(偏好設(shè)置)
      tmp 臨時(shí)文件夾,不進(jìn)行同步,當(dāng)設(shè)備重啟,就會(huì)清空。
     */
    
    
    // 拼接路徑獲取documents路徑
    NSString *path =  [sandBoxpath stringByAppendingPathComponent:@"Documents"];
   
    NSLog(@"%@",path);
    /**
     *  直接獲取路徑
     *
     *  @第一個(gè)參數(shù):尋找的哪一個(gè)文件夾
     *  @第二個(gè)參數(shù):用戶沙盒主目錄
     *  @第三個(gè)參數(shù):如果為 NO 則不能完成讀寫操作
     */
    NSString *path1 = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).lastObject;
    NSLog(@"%@",path1);
    
    
    //獲取preference  一般通過NSuserDefaults
    [[NSUserDefaults standardUserDefaults] setObject:@"UI" forKey:@"iOS"];
    NSLog(@"%@",[[NSUserDefaults standardUserDefaults] objectForKey:@"iOS"]);
    
    

    [self SimpleTypes];
  
    
    
    // Do any additional setup after loading the view, typically from a nib.
}

#pragma mark---------------簡(jiǎn)單數(shù)據(jù)寫入、讀取----------
- (void)SimpleTypes{
   //拼接寫入文件的文件路徑
    
    NSString *documentsPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).lastObject;
    
    NSString *filepath = [documentsPath stringByAppendingPathComponent:@"file.txt"];
    //吧String寫入文件
    NSString *string = @"老衲是文字";
    BOOL isWriting = [string writeToFile:filepath atomically:YES encoding:NSUTF8StringEncoding error:nil];
    if (isWriting) {
        NSLog(@"成功");
    }else{
        NSLog(@"失敗");
    }
    
    
    //讀取數(shù)據(jù)
    NSString *readString = [NSString stringWithContentsOfFile:filepath encoding:NSUTF8StringEncoding error:nil];
    
    NSLog(@"%@",readString);
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

打印結(jié)果

屏幕快照 2016-06-24 下午8.18.19.png
最后編輯于
?著作權(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)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

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

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