iOS開發-資料存放區

來源:互聯網
上載者:User

標籤:

緩衝的儲存:NSSearchPathForDirectoriesInDomains儲存:
   //這個方法擷取出的結果是一個數組.因為有可以搜尋到多個路徑.    NSArray *array =  NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);    //在這裡,我們指定搜尋的是Cache目錄,所以結果只有一個,取出Cache目錄    NSString *cachePath = array[0];    //拼接檔案路徑,stringByAppendingPathComponent會在cachePath路徑後面加上/再拼接    NSString *filePathName = [cachePath stringByAppendingPathComponent:@"agePlist.plist"];    //1.用字典寫, plist檔案當中儲存的是字典.    NSDictionary *dict = @{@"age" : @18,@"name" : @"gaowei"};    //ToFile:要寫入的沙箱路徑    [dict writeToFile:filePathName atomically:YES];    //2.用數組寫,plist檔案當中儲存的類型是數組    NSArray *dataArray = @[@56,@"asdfa"];    //ToFile:要寫入的沙箱路徑    [dataArray writeToFile:filePathName atomically:YES];

讀取:

    //這個方法擷取出的結果是一個數組.因為有可以搜尋到多個路徑.    NSArray *array =  NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);    //在這裡,我們指定搜尋的是Cache目錄,所以結果只有一個,取出Cache目錄    NSString *cachePath = array[0];    //拼接檔案路徑    NSString *filePathName = [cachePath stringByAppendingPathComponent:@"agePlist.plist"];    //從檔案當中讀取字典, 儲存的plist檔案就是一個字典,這裡直接填寫plist檔案所存的路徑    NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile:filePathName];    //如果儲存的是一個數組.那就通過數組從檔案當中載入.    NSArray *dataArray = [NSArray arrayWithContentsOfFile:filePathName];
喜好設定的儲存:NSUserDefaults儲存:
    //喜好設定NSUserDefaults    //底層就是封閉了一個字典,利用字典的方式產生plist檔案    //好處:不需要關心檔案名稱(它會自動產生)快速進行索引值對儲存.    NSUserDefaults *defautls = [NSUserDefaults standardUserDefaults];    [defautls setObject:@"gaowei" forKey:@"name"];    [defautls setBool:YES forKey:@"isBool"];    [defautls setInteger:5 forKey:@"num"];    //同步,立即寫入檔案.    [defautls synchronize];

讀取:

    //存是用什麼key存的, 讀的時候就要用什麼key值取    //存的時候使用的什麼類型,取的時候也要用什麼類型.    NSString *str = [[NSUserDefaults standardUserDefaults] objectForKey:@"name"];    BOOL isBool  = [[NSUserDefaults standardUserDefaults] boolForKey:@"isBool"];    NSInteger num = [[NSUserDefaults standardUserDefaults] integerForKey:@"num"];    NSLog(@"name =%@-isBool=%d-num=%ld",str,isBool,num);

歸檔的儲存:NSKeyedArchiver

與pist儲存,喜好設定儲存不同的是,歸檔可用於儲存自訂的類,而那倆者只能儲存系統的類的資料
    //擷取沙箱的路徑 person.data 隨便寫   NSString *path = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)[0];   NSString *filePathName =  [path stringByAppendingPathComponent:@"person.data"];//    Person *per = [[Person alloc] init];//    per.name = @"xmg";//    per.age = 10;    Student *stu = [[Student alloc] init];    stu.name = @"gxq";    stu.age = 10;    stu.height = 150;    Dog *dog = [[Dog alloc] init];    dog.name = @"wangcai";    stu.dog = dog;    //歸檔    //archiveRootObject執行這個方法時, 底層會調用要存的對象的encodeWithCoder方法,    //調用encodeWithCoder目的就是想詢問下要存對象的哪些屬性.怎麼存.    [NSKeyedArchiver archiveRootObject:stu toFile:filePathName];

讀取:

    //擷取沙箱的路徑    NSString *path = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)[0];    NSString *filePathName =  [path stringByAppendingPathComponent:@"person.data"];    //解檔    //unarchiveObjectWithFile執行這個方法時,底層會調用要存的對象的initWithCoder方法,    //調用initWithCoder目的就是想詢問下要取對象的哪些屬性.    Student *per = [NSKeyedUnarchiver unarchiveObjectWithFile:filePathName];
//注意點: 不管是自訂的類對象"per”,繼承的類對象”stu”,包含的類對象”dog”,只要歸檔的時候,都要實現encodeWithCoder,解檔就要實現initWithCoder//必須得要遵守NSCoding協議才行-(void)encodeWithCoder:(NSCoder *)encoder{    [encoder encodeObject:self.name forKey:@"name"];    [encoder encodeInt:self.age forKey:@"age"];}//當解析一個檔案時就會調用.-(instancetype)initWithCoder:(NSCoder *)decoder{    if (self = [super init]) {       self.name = [decoder decodeObjectForKey:@"name"];       self.age =  [decoder decodeIntForKey:@"age"];    }    return self;}

 

 

iOS開發-資料存放區

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.