標籤:
1、NSKeyarchiver
2、NSUserDefault
3、PList
4、Write
5、SQLite
6、KeyChain
樣本:
1、NSKeyarchiver
將資料存入檔案:
NSString *rootDir = NSHomeDirectory();NSString *path = [rootDir stringByAppendingPathComponent:@"test.txt"]; NSMutableData *data = [NSMutable data];NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingMutableData:data]; NSArray *array = @[@"one",@"two"];[archiver encodeInt:100 forKey:@"age"];//相當於SharedPreference的putInteger(key,value);[archiver encodeObject:array forKey:@"names"];//[archiver finishEncoding];//此方法調用,則將資料存入data[archiver release]; if([data writeToFile:path atomically:YES]){ NSLog(@"對象存入檔案成功");}
將資料從檔案中取出:
NSData *data = [NSData dataWithContentOfFile:path];NSKeyedArchiver *unarchiver =[[NSKeyedArchiver alloc] initForReadingWithData:data];int age = [unarchiver decodeIntForKey:@"age"];//取出值NSArray *array = [unarchiver decodeObjectForKey:@"names"];[archiver release];
2、NSUserDefault:用來儲存應用程式設定和屬性、使用者儲存的資料。使用者再次開啟程式或開機後這些資料仍然存在。NSUserDefaults可以儲存的資料類型包 括:NSData、NSString、NSNumber、NSDate、NSArray、NSDictionary。如果要儲存其他類型,則需要轉換為前 面的類型,才能用NSUserDefaults儲存。
//1. 存入資料NSArray *array = @[@"abc",@"d"];NSUserDefaults *userDefault = [NSUserDefaults standardUserDefaults];[userDefault setInteger:123 forKey:@"number"];[userDefault setObject:array forKey:@"array"];[userDefault synchronize];//存入檔案 //2. 取出資料NSInteger number = [userDefault integerForKey:@"number"];NSArray *array = [userDefault objectForKey:@"array"];
3、PList:儲存簡單基礎資料類型
4、Write:
第一步:擷取檔案即將儲存的路徑。
1 NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask,YES);2 NSString *ourDocumentPath =[documentPaths objectAtIndex:0];3 4 //使用C函數NSSearchPathForDirectoriesInDomains來獲得沙箱中目錄的全路徑。
該函數有三個參數,目錄類型、he domain mask、布爾值。其中布爾值表示是否需要通過~擴充路徑。
而且第一個參數是不變的,即為NSSearchPathDirectory 。在IOS中後兩個參數也是不變的,即為:NSUserDomainMask 和 YES。
或者使用NSHomeDirectory方法擷取sandbox路徑。
NSString *sandboxPath = NSHomeDirectory();NSString *documentPath = [sandboxPath stringByAppendingPathComponent:@"Documents"];//將Documents添加到sandbox路徑上
兩者的區別是:使用NSSearchPathForDirectoriesInDomains比在NSHomeDirectory後面添加Document更加安全。因為該檔案目錄可能在未來發送的系統上發生改變。
第二步:產生在該路徑下的檔案:
NSString *FileName=[documentDirectory stringByAppendingPathComponent:saveFileName];//saveFileName就是儲存檔案的檔案名稱
第三步:寫入資料
[data writeToFile:saveFileName atomically:YES];//將NSData類型對象data寫入檔案,檔案名稱為saveFileName
第四步:從檔案中讀取資料
NSData data=[NSData dataWithContentsOfFile:saveFileName options:0 error:NULL];//從saveFileName中讀取出資料
5、SQLite
第一步:需要添加SQLite相關的庫以及標頭檔:
在專案檔的Build Phases下,找到Link Binary Library(ies),添加libsqlite3.0.dylib(libsqlite3.dylib與前者的區別暫時不知,兩者應該差不多);在專案檔中標頭檔或者源檔案中添加標頭檔#import "/usr/include/sqlite3.h"。
第二步:使用SQLite:
NSArray *documentsPaths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask , YES);NSString *databaseFilePath=[[documentsPaths objectAtIndex:0] stringByAppendingPathComponent:@"mydb"];
//開啟資料庫
if (sqlite3_open([databaseFilePath UTF8String], &database)==SQLITE_OK)
{
NSLog(@"sqlite dadabase is opened.");
}
else{
return;
}//開啟不成功就返回
//在開啟了資料庫的前提下,如果資料庫沒有表,那就開始建表
char *error;
const char *createSql="create table(id integer primary keyautoincrement, name text)";
if (sqlite3_exec(database, createSql, NULL, NULL,&error)==SQLITE_OK) {
NSLog(@"create table is ok.");
}
else
{
NSLog(@"error: %s",error);
sqlite3_free(error);//每次使用完畢清空error字串,提供給下一次使用
}
//建表完成之後,就開始插入記錄:
const char *insertSql="insert into a person (name)values(‘gg’)";
if (sqlite3_exec(database, insertSql, NULL, NULL,&error)==SQLITE_OK) {
NSLog(@"insert operation is ok.");
}
else
{
NSLog(@"error: %s",error);
sqlite3_free(error);//每次使用完畢清空error字串,提供給下一次使用
}
//查詢記錄:
const char *selectSql="selectid,name from a person";
sqlite3_stmt *statement;
if (sqlite3_prepare_v2(database,selectSql, -1,&statement, nil)==SQLITE_OK){
NSLog(@"select operation is ok.");
}
else
{
NSLog(@"error: %s",error);
sqlite3_free(error);
}
while(sqlite3_step(statement)==SQLITE_ROW){
int _id=sqlite3_column_int(statement,0);
NSString *name=(char*)sqlite3_column_text(statement,1);
NSLog(@"row>>id %i, name%s",_id,name);
}
sqlite3_finalize(statement);
//最後,關閉資料庫:
sqlite3_close(database);
注意:寫入資料庫,字串可以採用char方式,而從資料庫中取出char類型,當char類型有表示中文字元時,會出現亂碼。這是因為資料庫預設使用ascII編碼方式。所以要想正確從資料庫中取出中文,需要用NSString來接收從資料庫取出的字串
ios資料存放區的幾種常用方式