資料儲存方式

2021-07-16 09:08:45 字數 1747 閱讀 1698

儲存資料:

一.檔案儲存:

nsuerdefault

,plist

檔案(不太靈活,一般儲存死資料),歸檔

1.nsuserdefault:用來儲存應用程式設定和屬性、使用者儲存的資料。使用者再次開啟程式或開機後這些資料仍然存在。nsuserdefaults可以儲存的資料型別包括:nsdata、nsstring、nsnumber、nsdate、nsarray、nsdictionary。

缺點:如果要儲存其他型別,需要轉換為前面的型別,才能用nsuserdefaults儲存。(自定義的資料型別需轉換成nsdata才行)

2.歸檔nskeyedarchiver

採用歸檔的形式來儲存資料,資料物件需要遵守nscoding協議,物件對應的類必須提供encodewithcoder:和initwithcoder:方法。

缺點:只能一次性歸檔儲存以及一次性解壓。所以只能針對小量資料,對資料操作比較笨拙,如果想改動資料的某一小部分,需要解壓或歸檔整個資料。

二.資料庫:coredata,fmdatabase第三方庫

資料庫使用步驟:建立資料庫物件———》開啟資料庫————》建立**create————》處理資料(增,刪,查,改)————》關閉資料庫

關於資料庫操作推薦大家關注部落格:

fmdatabase資料庫的依賴庫:【工程中必須匯入如下檔案,並使用 libsqlite3.dylib依賴包】

fmdatabase第三方資料庫的處理語句:

建立資料庫:

fmdatabase *db = [fmdatabase databasewithpath:path];

開啟資料庫:

[db open]  ;

建立**:nsstring *sqlcreatetable =  [nsstring stringwithformat:@"create table if not exists '%@' ('%@' integer primary key autoincrement, '%@' text, '%@' integer, '%@' text)",tablename,id,name,age,address]; 

增:nsstring *insertsql1= [nsstring stringwithformat:  @"insert into '%@' ('%@', '%@', '%@') values ('%@', '%@', '%@')",  tablename, name, age, address, @"張三", @"13", @"濟南"]; 

刪:nsstring *deletesql = [nsstring stringwithformat:  @"delete from %@ where %@ = '%@'",  tablename, name, @"張三"];

查:nsstring * sql = [nsstring stringwithformat:  @"select * from %@",tablename]; 

改:nsstring *updatesql = [nsstring stringwithformat:  @"update '%@' set '%@' = '%@' where '%@' = '%@'",  tablename,   age,  @"15" ,age,  @"13"];

關閉資料庫:

[db close];

三.讀寫的方式

write寫入方式:永久儲存在磁碟中。

第一步:獲得檔案即將儲存的路徑:

第二步:生成在該路徑下的檔案:

第三步:往檔案中寫入資料:

最後:從檔案中讀出資料:

資料儲存方式

最快速的但不可隨意擴充套件 陣列大小確定 array collection list linkedlist arraylist 動態陣列 vector stack set map hashtable hashmap 雜湊表 weakhashmap 可以按照索引查詢 有序 可以包含null值 多個 元...

資料儲存方式

不同系統使用的cpu不同,對資料的儲存形式也不同,分為兩種。1個十六進製制數 4個二進位制數 1bytes 8個二進位制數 1bytes 2個十六進製制數 例 int x 0x12345678 123左邊為高位,右邊為低位 低位址小端模式 大端模式 0xdff30 0x78 0x12 0xdff31...

C int 資料儲存方式

c 中int型的負數是以補碼的形式存在的。例如 正數是其自身,0 0111 1111 1111 1111 0 32767 負數則為其正數的取反 1 1 0000 0000 0000 0001 1111 1111 1111 1110 1 1111 1111 1111 1111 最小的負數為 32768...