標籤:
資料存放區的五種方法:
1、xml屬性列表(plist歸檔)
2、NSKeyedArchiver歸檔(加密形式)
3、NSUserDefaults(喜好設定)
4、SQLite(嵌入式資料庫)
5、Core Date(物件導向方式的嵌入式資料庫)
一、plist的讀寫
缺點:
1、明文儲存
2、操作對象有限只有NSArray、NSMutableArray、NSDictionary、NSMutableDictionary支援
(歸檔時只要調用對應的writeToFile方法即可,解檔調用arrayWithContentsOfFile或dictionaryWithContentsOfFile)
寫:
1、//擷取到Document檔案路徑
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
NSString *path = paths[0];
2、//建立plist檔案
NSString *filename = [path stringByAppendingPathComponent:@“test.plist”];
3、//建立Dictionary或者NSArray寫入
NSDictionary *dic1 = @{@“name”:@“zhang”,@“age”:@22,@“sex”:@“man”};
NSDictionary *dic2 = @{@“name”:@“li”,@“age”:@54,@“sex”:@“female”};
NSDictionary *dic3 = @{@“name”:@“wang”,@“age”:@25,@“sex”:@“man”};
NSDictionary *dic = @{@“z”:dic1,@“l”:dic2,@“w”:dic3};
4、//寫入到test.plist中
[dic writeToFile:filename atomically:YES];
讀:
1、//擷取到Document檔案路徑
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
NSString *path = paths[0];
2、//plist檔案
NSString *filename = [path stringByAppendingPathComponent:@“test.plist”];
3、//讀
NSDictionary *readDic = [NSDictionary dictionaryWithContentsOfFile:filename];
NSDictionary *person = readDic[@“z”];
NSString *name = person[@“name”];
二、NSKeyedArchiver歸檔(加密形式)
分為簡單歸檔(針對單個對象,不用設定key)和複雜物件歸檔(針對多個對象,需要設定不同的key)
OS X為:NSArchiver 和 NSUnarchiver
iOS 為:NSKeyedArchiver 和 NSKeyedUnarchiver
1、簡單歸檔:
NSString *str = @“hello”;
//要歸檔的內容
//也可以是NSArray *array = @[@“a”,@“b”,@“c”];
NSArray *array = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
NSString *path = array[0];
NSString *filename = [path stringByAppendingPathComponent:@"test.arc"];
//獲得路徑
if(![NSKeyArchiver archiveRootObject:str toFile:filename])
{
NSLog(@“archiver failed!”);
}
//歸檔
(id) st = [NSKeyedUnarchiver unarchiverObjectWithFile:filename];
//解檔
//(上面的id指任意的對象,可以是NSString,也可以是NSArray);
2、複雜物件歸檔(多個物件歸檔)
//存檔地址
NSString *filename2 = [path stringByAppendingPathComponent:@"test2.arc"];
歸檔:
//設定資料
int myInt = 31;
NSString *myString = @"hello”;
NSNumber *myNumber = @32;
NSArray *myArray = @[@"ok",@"no",@"byel"];
NSDictionary *myDic = @{@"dic1":@"a",@"dic2":@"b",@"dic3":@"c"};
//建立臨時存放資料的data
NSMutableData *data = [[NSMutableData alloc]init];
//設定存檔儲存在data對象中
NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc]initForWritingWithMutableData:data];
//對對象進行歸檔,並設定key以便讀取
[archiver encodeInt:myInt forKey:@"int"];
[archiver encodeObject:myString forKey:@"string"];
[archiver encodeObject:myNumber forKey:@"number"];
[archiver encodeObject:myArray forKey:@"array"];
[archiver encodeObject:myDic forKey:@"dictionary"];
//結束存檔
[archiver finishEncoding];
//寫入檔案
[data writeToFile:filename2 atomically:YES];
讀檔:
//定義資料
int myInt2;
NSString *myString2;
NSNumber *myNumber2;
NSArray *myArray2;
NSDictionary *myDic2;
//擷取到檔案,存放在data2中
NSMutableData *data2 = [[NSMutableData alloc]initWithContentsOfFile:filename2];
//定義解檔data2
NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc]initForReadingWithData:data2];
//解檔並存放在變數中
myInt2 = [unarchiver decodeIntForKey:@"int"];
myString2 = [unarchiver decodeObjectForKey:@"string"];
myNumber2 = [unarchiver decodeObjectForKey:@"number"];
myArray2 = [unarchiver decodeObjectForKey:@"array"];
myDic2 = [unarchiver decodeObjectForKey:@"dictionary"];
//結束解檔
[unarchiver finishDecoding];
三、NSUserDefaults
NSUserDefaults支援的資料類型有:NSNumber(NSInteger、float、double),NSString,NSDate,NSArray,NSDictionary,BOOL.
1、存資料:
//將NSString Object Storage Service到 NSUserDefaults 中
NSString *passWord = @"1234567";
NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
[user setObject:passWord forKey:@"userPassWord"];
2、取資料
NSUserDefaults *user = [NSUserDefaults standardUserDefaults];
NSString *passWord = [ user objectForKey:@"userPassWord"];
//存自訂的資料用NSData
四、SQLite資料庫
特點:
1、在iOS中需要使用C語言文法進行資料庫操作、訪問(無法使用ObjC直接存取,因為libsqlite3架構基於C語言編寫)
2、建立串連後通常不需要關閉串連(可以手動關閉)
步驟:(先匯入libsqlite3架構)
1、開啟資料庫。
//用sqlite_open()開啟資料庫會指定一個資料庫檔案儲存路徑,如果檔案存在則直接開啟,否則建立並開啟。(開啟資料庫會得到一個sqlite3類型的對象,後面需要藉助這個對象進行其他動作)
2、執行SQL語句。
執行SQL語句包括有傳回值的語句和無傳回值語句。
(1)、對於有傳回值的語句(如增刪改等操作)直接通過sqlite3_exec()函數執行;
(2)、對於有傳回值的語句(如查詢操作)則首先通過sqlite3_prepare_v2()執行文法檢測,然後通過sqlite3_step()依次取出查詢結果的每一行資料,對於每行資料都可以通過對應的sqlite_column_類型名()方法獲得對應列的資料,如此反覆迴圈直到遍曆完成。最後通過sqlite3_finalize()釋放。
代碼:
1、建立資料庫
NSString *path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)objectAtIndex:0];
NSString *filename = [path stringByAppendingPathComponent:dbName];
2、開啟資料庫並判斷是否開啟
if (sqlite3_open([filename UTF8String], &database) == SQLITE_OK) {
NSLog(@"database open success");
}
else{
NSLog(@"database open failure");
}
3、處理一個沒有傳回值的sql語句(如增刪改)
if (sqlite3_exec(database, [sql UTF8String], nil, nil, &error) != SQLITE_OK){
NSLog(@"%s",error);
}
4、處理一個有傳回值的sql語句(如查詢)
-(NSArray *)executeQuery:(NSString *)sql{
NSMutableArray *array = [[NSMutableArray alloc]init];
sqlite3_stmt *stmt;
int result = sqlite3_prepare_v2(database, [sql UTF8String], -1, &stmt, NULL);
if (result == SQLITE_OK) {
while (sqlite3_step(stmt) == SQLITE_ROW) {
NSMutableDictionary *dic = [[NSMutableDictionary alloc]init];
for (int i = 0; i < sqlite3_column_count(stmt); i ++) {
NSString *columnName =[NSString stringWithUTF8String:sqlite3_column_name(stmt, i)];
NSString *value=[NSString stringWithUTF8String:(char *)sqlite3_column_text(stmt, i)];
dic[columnName] = value;
}
[array addObject:dic];
}
}
return array;
}
5、關閉資料庫
if(sqlite3_close(database) != SQLITE_OK){
NSLog(@"%s",sqlite3_errmsg(database));
}
簡單的資料存放區——plist、NSKeyedArchiver歸檔、NSUserDefaults(喜好設定)、SQLite(嵌入式資料庫)