ios中的四種數據存儲方式
iphone沙箱模型的有四個文件夾,分別是什麼,永久數據存儲一般放在什麼位置,得到模擬器的路徑的簡單方式是什麼.
documents,tmp,app,Library。(NSHomeDirectory()),手動保存的文件在documents文件裏,Nsuserdefaults保存的文件在tmp文件夾裏。
1、Documents 目錄:您應該將所有de應用程序數據文件寫入到這個目錄下。這個目錄用於存儲用戶數據或其它應該定期備份的信息。
2、AppName.app 目錄:這是應用程序的程序包目錄,包含應用程序的本身。由於應用程序必須經過簽名,所以您在運行時不能對這個目錄中的內容進行修改,否則可能會使應用程序無法啓動。
3、Library 目錄:這個目錄下有兩個子目錄:Caches 和 Preferences
Preferences 目錄:包含應用程序的偏好設置文件。您不應該直接創建偏好設置文件,而是應該使用NSUserDefaults類來取得和設置應用程序的偏好.
Caches 目錄:用於存放應用程序專用的支持文件,保存應用程序再次啓動過程中需要的信息。
4、tmp 目錄:這個目錄用於存放臨時文件,保存應用程序再次啓動過程中不需要的信息。
獲取這些目錄路徑的方法:
1,獲取家目錄路徑的函數:
NSString *homeDir = NSHomeDirectory();
2,獲取Documents目錄路徑的方法:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docDir = [paths> NSString *cachesDir = [paths objectAtIndex:0];
4,獲取tmp目錄路徑的方法:
NSString *tmpDir = NSTemporaryDirectory();
5,獲取應用程序程序包中資源文件路徑的方法:
例如獲取程序包中一個圖片資源(apple.png)路徑的方法:
NSString *imagePath = [[NSBundle> 代碼中的mainBundle類方法用於返回一個代表應用程序包的對象。
1.NSKeyedArchiver
說明:採用歸檔的形式來保存數據,該數據對象需要遵守NSCoding協議,並且該對象對應的類必須提供encodeWithCoder:和initWithCoder:方法。前一個方法告訴系統怎麼對對象進行編碼,而後一個方法則是告訴系統怎麼對對象進行解碼。
儲存文件:
+ (void)saveData:(id)object>
return ;
}
NSString *filePath = [[LFRDetailViewControllergetPath]stringByAppendingString:
[NSStringstringWithFormat:@"/%@.txt",aFileName]];
NSMutableData *data = [[NSMutableDataalloc]init];
NSKeyedArchiver *archiver = [[NSKeyedArchiveralloc]initForWritingWithMutableData:data];
[archiver>
[NSStringstringWithFormat:@"/%@.txt",aFileName]];
NSData *datas = [NSDatadataWithContentsOfFile:filePath];
__autoreleasingNSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiveralloc]initForReadingWithData:datas];
>
[unarchiver> NSString *name =@”default string“;
[defaults> NSString *name = [defaults objectForKey:@"name"];//根據鍵值取出name
3. Write寫入方式
說明:永久保存在磁盤中。使用NSSearchPathForDirectoriesInDomains比在NSHomeDirectory後面添加Document更加安全。因爲該文件目錄可能在未來發送的系統上發生改變。具體方法爲:
//創建文件夾
-(void)createDir{
NSString *documentsPath =[selfdirDoc];
NSFileManager *fileManager = [NSFileManagerdefaultManager];
NSString *testDirectory = [documentsPathstringByAppendingPathComponent:@"test"];
// 創建目錄
BOOL>
>
NSString *testDirectory = [documentsPathstringByAppendingPathComponent:@"test"];
NSString *testPath = [testDirectorystringByAppendingPathComponent:@"test.txt"];
BOOL>
> NSString *databaseFilePath=[[documentsPaths objectAtIndex:0] stringByAppendingPathComponent:@"mydb"];
//上面兩句已經比較熟悉了吧!
//打開數據庫
if (sqlite3_open([databaseFilePath UTF8String], &database)==SQLITE_OK) {
NSLog(@"sqlite> if (sqlite3_exec(database, createSql, NULL, NULL, &error)==SQLITE_OK) {
NSLog(@"create> if (sqlite3_exec(database, insertSql, NULL, NULL, &error)==SQLITE_OK) {
NSLog(@"insert> sqlite3_stmt *statement;
if (sqlite3_prepare_v2(database,selectSql, -1, &statement,> NSLog(@"select operation is ok.");
}
else
{
NSLog(@"error: %s",error);
> 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來接收從數據庫取出的字符串。
案例使用說明;
[_sqliteopenDataBase]; //數據庫打開
[_sqlitecreateStudentTable]; //創建表
[_sqliteinsertName:stu]; //插入數據
[_sqlitegetAllData];//查詢表中的數據
documents,tmp,app,Library。(NSHomeDirectory()),手動保存的文件在documents文件裏,Nsuserdefaults保存的文件在tmp文件夾裏。
1、Documents 目錄:您應該將所有de應用程序數據文件寫入到這個目錄下。這個目錄用於存儲用戶數據或其它應該定期備份的信息。
2、AppName.app 目錄:這是應用程序的程序包目錄,包含應用程序的本身。由於應用程序必須經過簽名,所以您在運行時不能對這個目錄中的內容進行修改,否則可能會使應用程序無法啓動。
3、Library 目錄:這個目錄下有兩個子目錄:Caches 和 Preferences
Preferences 目錄:包含應用程序的偏好設置文件。您不應該直接創建偏好設置文件,而是應該使用NSUserDefaults類來取得和設置應用程序的偏好.
Caches 目錄:用於存放應用程序專用的支持文件,保存應用程序再次啓動過程中需要的信息。
4、tmp 目錄:這個目錄用於存放臨時文件,保存應用程序再次啓動過程中不需要的信息。
獲取這些目錄路徑的方法:
1,獲取家目錄路徑的函數:
NSString *homeDir = NSHomeDirectory();
2,獲取Documents目錄路徑的方法:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *docDir = [paths> NSString *cachesDir = [paths objectAtIndex:0];
4,獲取tmp目錄路徑的方法:
NSString *tmpDir = NSTemporaryDirectory();
5,獲取應用程序程序包中資源文件路徑的方法:
例如獲取程序包中一個圖片資源(apple.png)路徑的方法:
NSString *imagePath = [[NSBundle> 代碼中的mainBundle類方法用於返回一個代表應用程序包的對象。
1.NSKeyedArchiver
說明:採用歸檔的形式來保存數據,該數據對象需要遵守NSCoding協議,並且該對象對應的類必須提供encodeWithCoder:和initWithCoder:方法。前一個方法告訴系統怎麼對對象進行編碼,而後一個方法則是告訴系統怎麼對對象進行解碼。
儲存文件:
+ (void)saveData:(id)object>
return ;
}
NSString *filePath = [[LFRDetailViewControllergetPath]stringByAppendingString:
[NSStringstringWithFormat:@"/%@.txt",aFileName]];
NSMutableData *data = [[NSMutableDataalloc]init];
NSKeyedArchiver *archiver = [[NSKeyedArchiveralloc]initForWritingWithMutableData:data];
[archiver>
[NSStringstringWithFormat:@"/%@.txt",aFileName]];
NSData *datas = [NSDatadataWithContentsOfFile:filePath];
__autoreleasingNSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiveralloc]initForReadingWithData:datas];
>
[unarchiver> NSString *name =@”default string“;
[defaults> NSString *name = [defaults objectForKey:@"name"];//根據鍵值取出name
3. Write寫入方式
說明:永久保存在磁盤中。使用NSSearchPathForDirectoriesInDomains比在NSHomeDirectory後面添加Document更加安全。因爲該文件目錄可能在未來發送的系統上發生改變。具體方法爲:
//創建文件夾
-(void)createDir{
NSString *documentsPath =[selfdirDoc];
NSFileManager *fileManager = [NSFileManagerdefaultManager];
NSString *testDirectory = [documentsPathstringByAppendingPathComponent:@"test"];
// 創建目錄
BOOL>
>
NSString *testDirectory = [documentsPathstringByAppendingPathComponent:@"test"];
NSString *testPath = [testDirectorystringByAppendingPathComponent:@"test.txt"];
BOOL>
> NSString *databaseFilePath=[[documentsPaths objectAtIndex:0] stringByAppendingPathComponent:@"mydb"];
//上面兩句已經比較熟悉了吧!
//打開數據庫
if (sqlite3_open([databaseFilePath UTF8String], &database)==SQLITE_OK) {
NSLog(@"sqlite> if (sqlite3_exec(database, createSql, NULL, NULL, &error)==SQLITE_OK) {
NSLog(@"create> if (sqlite3_exec(database, insertSql, NULL, NULL, &error)==SQLITE_OK) {
NSLog(@"insert> sqlite3_stmt *statement;
if (sqlite3_prepare_v2(database,selectSql, -1, &statement,> NSLog(@"select operation is ok.");
}
else
{
NSLog(@"error: %s",error);
> 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來接收從數據庫取出的字符串。
案例使用說明;
[_sqliteopenDataBase]; //數據庫打開
[_sqlitecreateStudentTable]; //創建表
[_sqliteinsertName:stu]; //插入數據
[_sqlitegetAllData];//查詢表中的數據
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.