儲存到本地的資料型別有:陣列,字典,字串,物件型別的;
1 字串的本地儲存:
nsstring
*str =@"dsadasd";
nsarray*arr =nssearchpathfordirectoriesindomains(nslibrarydirectory,nsuserdomainmask,
yes);
nsstring *path = [arr firstobject];
nsstring*stringpath = [nsstringstringwithformat:
@"%@/hello"
, path];
[strwritetofile:stringpathatomically:
yesencoding:nsutf8stringencodingerror:
nil];
nslog(@"%@", path);
資料的讀取:
nsstring*path = [nssearchpathfordirectoriesindomains(nsdocumentdirectory,nsuserdomainmask,
yes)firstobject];
path = [nsstringstringwithformat:
@"%@/hello.text"
, path];
nsstring*str = [nsstringstringwithcontentsoffile:pathencoding:nsutf8stringencodingerror:
nil];
nslog(@"%@", str);
2 jason型別的本地儲存(陣列,字典)
nsarray*arr =nssearchpathfordirectoriesindomains(nslibrarydirectory,nsuserdomainmask,
yes);
nsstring *path = [arr firstobject];
nsstring *path = [arr firstobject];
nsarray*a = [nsarrayarraywithobjects:
@"dsa"
,@"dad"
,nil
];nsstring*arrpath = [nsstringstringwithformat:
@"%@/arr"
, path];
[awritetofile:arrpathatomically:
yes];
資料的讀取:
nsarray *array = [nsarray
arraywithcontentsoffile:arrpath];
3 nsdata的本地儲存
nsarray*arr =nssearchpathfordirectoriesindomains(nslibrarydirectory,nsuserdomainmask,
yes);
nsstring *path = [arr firstobject];
字串轉換為nsdata型別
nsstring *str =@"sadas d";
nsdata*data = [strdatausingencoding:nsutf8stringencoding];
nsstring*datapath = [nsstringstringwithformat:
@"%@/data"
, path];
[datawritetofile:datapath atomically:yes];
jason型別轉換為nsdata型別
nsmutablearray*arr = [nsmutablearrayarraywithobjects:
@"1"
,@"2"
,@"4"
, @"5"
,nil
];nsdata*data = [nsjsonserializationdatawithjsonobject:arroptions:nsjsonwritingprettyprintederror:
nil];
轉換為nsdata型別
data資料型別的讀取
nsstring*path = [nssearchpathfordirectoriesindomains(nslibrarydirectory,nsuserdomainmask,
yes)firstobject];
path = [nsstringstringwithformat:
@"%@/arrdata"
, path];
nsdata*data = [nsdatadatawithcontentsoffile:path];
nslog(@"%@", data);
// 把data轉換乘字串
nsstring*str =[[nsstringalloc]initwithdata:dataencoding:nsutf8stringencoding];
nslog(@"%@", str);
// data轉換成json
nsarray*arr = [nsjsonserializationjsonobjectwithdata:dataoptions:nsjsonreadingmutablecontainerserror:
nil];
nslog(@"%@", arr);
4 類的本地儲存
model型別物件儲存:
1 先進行歸檔/解檔(序列化,反序列化)使用協議 nscode
// 歸檔或者序列化 將屬性按照一定的格式分裝起來 便於轉換成nsdata
- (void)encodewithcoder:(nscoder *)acoder
// 解檔或者反序列化 將屬性從data中取出來
- (id)initwithcoder:(nscoder *)adecoder
return self;
}
2把物件轉換成nadata,然後再儲存
student*stu = [studentstudentwithname:
@"李四"
***:
@"男"
hobby:
@"女"
age:18];
nsdata*data = [nskeyedarchiverarchiveddatawithrootobject:stu];
nsstring*path = [nssearchpathfordirectoriesindomains(nslibrarydirectory,nsuserdomainmask,
yes)firstobject];
path = [nsstringstringwithformat:
@"%@/stu.text"
, path];
[datawritetofile:pathatomically:
yes];
類的讀取
student*stu = [nskeyedunarchiverunarchiveobjectwithfile:path];
本地儲存資料
直接將資料寫在 裡,不是一種合理的做法,如果資料經常改,就要經常翻開對應的 進行修改,造成 擴充套件型低 因此,可以考慮將經常變的資料放在檔案中進行儲存,程式啟動後從檔案中讀取最新的資料,如果要變動資料,直接修改資料檔案即可,不用修改 一般可以使用屬性列表檔案儲存nsarray或者dictionar...
android 本地儲存資料
string prefs name lmn.com string itemuser username string itempass password string itemcheck check 一 儲存 sharedpreferences settings getsharedpreference...
前端本地儲存的方法
本地儲存有三種方法localstorage sessionstorage cookie cookie存值大小4kb,使用時得封裝,預設儲存時間7天 localstorage存值5mb,儲存時間永久 sessionstorage為會話級別,頁面關閉以後會銷毀 存 let ls localstorage...