資料持久化 之 沙盒機制 sandBox

2021-07-08 19:01:09 字數 1287 閱讀 1514

- (void)viewdidload  else 

// *** 寫個陣列到本地

nsmutablearray *array = [nsmutablearray arraywithobjects:@"王金波", @"王浩", @"張天", @"早日天", nil];

bool result1 = [array writetofile:stuarraypath atomically:yes];

if (result1) else

// *** 寫入字典到本地

// 這裡用字面量, 字面量只能用不可變字典

// @"":字串 @{}: 字典 @: 陣列 @() : nsnumber

// @() 通常是將基本資料型別轉換成物件型別,相當於nsnumber

nsdictionary *dic = @;

// nsdictionary *dic = [nsdictionary dictionarywithobjectsandkeys:@"李添卓", @"name", nil];

bool result2 = [dic writetofile:dicpth atomically:yes];

if (result2) else

// *** 歸檔: 就是將複雜型別的資料,比如model,student,teacher 類 寫入到本地

// 反歸檔: 從本地取出歸檔的資料

#pragma mark -- 歸檔 4 步: 建立model物件,進行歸檔操作

student *stu = [[student alloc]init];

stu.name = @"王建業";

stu.gender = @"男";

stu.age = 18;

// nskeyedarchiver 歸檔類

// 歸檔操作

// 引數 1:model 物件

// 引數 2:是路徑

[nskeyedarchiver archiverootobject:stu tofile:stufilepath];

// 反歸檔, 取出model物件

student *stu1 = [nskeyedunarchiver unarchiveobjectwithfile:stufilepath];

nslog(@"name = %@, gender = %@ , age = %ld", stu1.name, stu1.gender, stu1.age);

iOS 沙盒 資料持久化

注意在這個筆記裡有部分路徑為方法,需要自己定義,在這裡一定要熟練使用獲取資料夾路徑以及路徑的拼接 什麼是資料持久化 資料的永久儲存 為什麼要做資料持久化 儲存在記憶體中的資料,程式關閉,記憶體釋放,資料丟失。這種資料是臨時的。資料持久化的本質 資料儲存成檔案,儲存到程式的沙盒裡 1.每個應用程式位於...

iOS中的UI資料持久化,沙盒

rootviewcontroller.m ui18 01資料持久化 created by rickie lambert on 15 12 7.import rootviewcontroller.h inte ce rootviewcontroller end implementation rootv...

iOS之沙盒 Sandbox 機制

ios的安全性結構可以在某種程式中保護應用程式的資料和系統免受安全漏洞的損害,ios提供了一種機制使得每個應用程式都在自己的沙盒下。1 沙盒規定了應用程式只能在為該應用建立的資料夾下讀取檔案,不能訪問其他應用程式的沙盒內容。2 應用程式間不能共享資料,沙盒裡的檔案不能被複製到其他應用程式中,也不能把...