国产成人精品a视频一区www_国产区视频在线观看_99色视频_欲色av_亚洲一区电影_亚洲综合视频一区

深入理解iPhone數(shù)據(jù)持久化

來(lái)源:網(wǎng)絡(luò)

點(diǎn)擊:1263

A+ A-

所屬頻道:新聞中心

關(guān)鍵詞: iPhone,數(shù)據(jù)持久化,開(kāi)發(fā)

      在所有的移動(dòng)開(kāi)發(fā)平臺(tái)數(shù)據(jù)持久化都是很重要的部分:在j2me中是rms或保存在應(yīng)用程序的目錄中,在symbian中可以保存在相應(yīng)的磁盤(pán)目錄中和數(shù)據(jù)庫(kù)中。symbian中因?yàn)闄?quán)限認(rèn)證的原因,在3rd上大多數(shù)只能訪問(wèn)應(yīng)用程序的private目錄或其它系統(tǒng)共享目錄。在iphone中,apple博采眾長(zhǎng),提供了多種數(shù)據(jù)持久化的方法,下面筆者會(huì)逐個(gè)進(jìn)行詳細(xì)的講解。

      iphone提供的數(shù)據(jù)持久化的方法,從數(shù)據(jù)保存的方式上講可以分為三大部分:屬性列表、對(duì)象歸檔、嵌入式數(shù)據(jù)庫(kù)(SQLite3)、其他方法。

      一、屬性列表NSUserDefaults

      NSUserDefaults類(lèi)的使用和NSKeyedArchiver有很多類(lèi)似之處,但是查看NSUserDefaults的定義可以看出,NSUserDefaults直接繼承自NSObject而NSKeyedArchiver 繼承自NSCoder。這意味著NSKeyedArchiver實(shí)際上是個(gè)歸檔持久化的類(lèi),也就可以使用NSCoder類(lèi)的[encodeObject: (id)objv forKey:(NSString *)key]方法來(lái)對(duì)數(shù)據(jù)進(jìn)行持久化存儲(chǔ)。

      - (void)applicationDidFinishLaunching:(UIApplication *)application {

      NSString *strOne = @“Persistent data1”;

      NSString *strTwo = @“Persistent data 2”;

      NSMutableArray *persistentArray = [[NSMutableArray alloc] init];

      [persistentArray addObject:strOne];

      [persistentArray addObject:strTwo];

      //archive

      NSUserDefaults *persistentDefaults = [NSUserDefaults standardUserDefaults];

      [persistentDefaults setObject:persistentArray forKey:@“myDefault”];

      NSString *descriptionDefault = [persistentDefaults description];

      NSLog(@“NSUserDefaults description is :%@”,descriptionDefault);

      //unarchive

      NSArray *UnpersistentArray =

      [persistentDefaults objectForKey:@“myDefault”];

      NSString *UnstrOne = [UnpersistentArray objectAtIndex:0];

      NSString *UnstrTwo = [UnpersistentArray objectAtIndex:1];

      NSLog(@“UnstrOne = %@,UnstrTwo = %@”,UnstrOne,UnstrTwo);

      // Override point for customization after application launch

      [window makeKeyAndVisible];

      }

      二、對(duì)象歸檔NSKeyedArchiver和NSKeyedUnarchiver

      iPhone和symbian 3rd一樣,會(huì)為每一個(gè)應(yīng)用程序生成一個(gè)私有目錄,這個(gè)目錄位于

      /Users/sundfsun2009/Library/Application Support/iPhone Simulator/User/Applications下,并隨即生成一個(gè)數(shù)字字母串作為目錄名,在每一次應(yīng)用程序啟動(dòng)時(shí),這個(gè)字母數(shù)字串都是不同于上一次的,上一次的應(yīng)用程序目錄信息被轉(zhuǎn)換成名為.DS_Store隱藏文件,這個(gè)目錄的文件結(jié)構(gòu)如下圖:

      通常使用Documents目錄進(jìn)行數(shù)據(jù)持久化的保存,而這個(gè)Documents目錄可以通過(guò)NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserdomainMask,YES)得到,代碼如下:

      - (void)applicationDidFinishLaunching:(UIApplication *)application {

      NSString *strOne = @“Persistent data1”;

      NSString *strTwo = @“Persistent data 2”;

      NSArray *persistentArray = [NSArray arrayWithObjects:strOne,strTwo,nil];

      NSArray *pathArray = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSAllDomainsMask, YES);

      int pathLen = [pathArray count];

      NSLog(@“path number is :%d”,pathLen);

      NSString *filePath;

      for(int i = 0; i 《 pathLen; i++)

      {

      filePath = [pathArray objectAtIndex:i];

      NSLog(@“%d path is :%@”,i,filePath);

      }

      NSString *myFilename = [filePath stringByAppendingPathComponent:@“myFile.rtf”];

      NSLog(@“myfile‘s path is :%@”,myFilename);

      // no files generated in correspond directory now

      [NSKeyedArchiver archiveRootObject:persistentArray toFile:myFilename];

      // now the myFile.rtf is generated

      // Override point for customization after application launch

      [window makeKeyAndVisible];

      }

      NSSearchPathForDirectoriesInDomains()的第二個(gè)參數(shù)是個(gè)枚舉值,在筆者的測(cè)試代碼中,只有NSUserDomainMask和NSAllDomainsMask可以獲取到目錄數(shù)為1,其余的皆為0,打印出來(lái)的結(jié)果如下:

      [Session started at 2009-11-10 21:30:08 +0800.]

      2009-11-10 21:30:10.516 PersistentExample[763:207] path number is :1

      2009-11-10 21:30:10.518 PersistentExample[763:207] 0 path is :/Users/sundfsun2009/Library/Application Support/iPhone Simulator/User/Applications/C93DC783-F137-4660-AE5A-08C3E11C774B/Documents

      2009-11-10 21:30:10.521 PersistentExample[763:207] myfile’s path is :/Users/sundfsun2009/Library/Application Support/iPhone Simulator/User/Applications/C93DC783-F137-4660-AE5A-08C3E11C774B/Documents/myFile.rtf

      Terminating in response to SpringBoard‘s termination.

      [Session started at 2009-11-10 21:32:27 +0800.]

      2009-11-10 21:32:30.091 PersistentExample[803:207] path number is :1

      2009-11-10 21:32:30.092 PersistentExample[803:207] 0 path is :/Users/sundfsun2009/Library/Application Support/iPhone Simulator/User/Applications/763E6772-E754-452F-8532-80C2CE4466B5/Documents

      2009-11-10 21:32:30.100 PersistentExample[803:207] myfile’s path is :/Users/sundfsun2009/Library/Application Support/iPhone Simulator/User/Applications/763E6772-E754-452F-8532-80C2CE4466B5/Documents/myFile.rtf

      Terminating in response to SpringBoard‘s termination.

      從打印的結(jié)果如下,每次應(yīng)用程序啟動(dòng)時(shí)生成的數(shù)字字母串目錄名字并不一樣。在調(diào)用[NSKeyedArchiver archiveRootObject:persistentArray toFile:myFilename]方法前,文件myFile.rtf并每生成,只有在調(diào)用此方法后才產(chǎn)生相應(yīng)的文件。

      下面需要把數(shù)據(jù)從屬性列表中讀取出來(lái),在上面的代碼中,筆者使用NSArray保存數(shù)據(jù)。但在大多數(shù)應(yīng)用程序中,數(shù)據(jù)的尺寸并不是固定的,這個(gè)時(shí)候就需要使用NSMutalbeArray動(dòng)態(tài)的保存數(shù)據(jù),代碼優(yōu)化如下:

      - (void)applicationDidFinishLaunching:(UIApplication *)application {

      NSString *myFilename;

      // archive

      {

      NSString *strOne = @“Persistent data1”;

      NSString *strTwo = @“Persistent data 2”;

      NSMutableArray *persistentArray = [[NSMutableArray alloc] init];

      [persistentArray addObject:strOne];

      [persistentArray addObject:strTwo];

      NSArray *pathArray = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSAllDomainsMask, YES);

      int pathLen = [pathArray count];

      NSLog(@“path number is :%d”,pathLen);

      NSString *filePath;

      for(int i = 0; i 《 pathLen; i++)

      {

      filePath = [pathArray objectAtIndex:i];

      NSLog(@“%d path is :%@”,i,filePath);

      }

      myFilename = [filePath stringByAppendingPathComponent:@“myFile.rtf”];

      NSLog(@“myfile’s path is :%@”,myFilename);

      [NSKeyedArchiver archiveRootObject:persistentArray toFile:myFilename];

      }

      // unarchive

      {

      NSArray *unarchiveArray = [NSKeyedUnarchiver unarchiveObjectWithFile:myFilename];

      NSString *UnstrOne = [unarchiveArray objectAtIndex:0];

      NSString *UnstrTwo = [unarchiveArray objectAtIndex:1];

      NSLog(@“UnstrOne = %@,UnstrTwo = %@”,UnstrOne,UnstrTwo);

      }

      // Override point for customization after application launch

      [window makeKeyAndVisible];

      }

      輸出結(jié)果如下:

      [Session started at 2009-11-10 22:41:57 +0800.]

      2009-11-10 22:41:59.344 PersistentExample[1082:207] path number is :1

      2009-11-10 22:41:59.346 PersistentExample[1082:207] 0 path is :/Users/sundfsun2009/Library/Application Support/iPhone Simulator/User/Applications/055CD17C-864E-4A83-ABF0-5F01EE85BD5A/Documents

      2009-11-10 22:41:59.355 PersistentExample[1082:207] myfile‘s path is :/Users/sundfsun2009/Library/Application Support/iPhone Simulator/User/Applications/055CD17C-864E-4A83-ABF0-5F01EE85BD5A/Documents/myFile.rtf

      2009-11-10 22:41:59.357 PersistentExample[1082:207] UnstrOne = Persistent data1,UnstrTwo = Persistent data 2

      Terminating in response to SpringBoard’s termination.

      從上面的圖中可以看到,目錄中還有個(gè)tmp目錄,讀者也可以把數(shù)據(jù)保存在tmp目錄中,獲取這個(gè)目錄使用NSTemporaryDirectory()方法。

      三、嵌入式數(shù)據(jù)庫(kù)(SQLite3)

      嵌入式數(shù)據(jù)庫(kù)持久化數(shù)據(jù)就是把數(shù)據(jù)保存在iphone的嵌入式數(shù)據(jù)庫(kù)系統(tǒng)SQLite3中,本質(zhì)上來(lái)說(shuō),數(shù)據(jù)庫(kù)持久化操作是基于文件持久化基礎(chǔ)之上的。

      要使用嵌入式數(shù)據(jù)庫(kù)SQLite3,首先需要加載其動(dòng)態(tài)庫(kù)libsqlite3.dylib,這個(gè)文件位于/Xcode3.1.4/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS3.1.sdk/usr/lib目錄下。在Framework文件夾上右擊,選擇“Adding-》Existing Files.。.”,定位到上述目錄并加載到文件夾。

      首先在頭文件中做如下修改:

      #import 《UIKit/UIKit.h》

      #include “sqlite3.h”

      #define kFileName @“mydb.sql”

      @interface PersistentExampleAppDelegate : NSObject 《UIApplicationDelegate》 {

      sqlite3 *database;

      UIWindow *window;

      }

      @property (nonatomic, retain) IBOutlet UIWindow *window;

      @end

      - (void)applicationDidFinishLaunching:(UIApplication *)application {

      NSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

      NSString *paths = [[path objectAtIndex:0] stringByAppendingPathComponent:kFileName];

      NSFileManager *fileManager = [NSFileManager defaultManager];

      BOOL findFile = [fileManager fileExistsAtPath:paths];

      NSLog(@“Database file path = %@”,paths);

      // 如果找到了數(shù)據(jù)庫(kù)文件

      if(findFile)

      {

      NSLog(@“Database file have already existed.”);

      if(sqlite3_open([paths UTF8String], &database) != SQLITE_OK)//打開(kāi)數(shù)據(jù)庫(kù)失敗

      {

      sqlite3_close(database);

      NSAssert(0,@“Failed to open database”);

      }

      }else

      {

      NSLog(@“Database file does not exsit!”);

      if(sqlite3_open([paths UTF8String], &database) != SQLITE_OK)//打開(kāi)數(shù)據(jù)庫(kù)失敗

      {

      sqlite3_close(database);

      NSAssert(0,@“Failed to open database”);

      }

      }

      char *errorMsg;

      //創(chuàng)建表

      NSString *createSQL = @“create table if not exists fields (row integer primary key, field_data text);”;

      if(sqlite3_exec(database, [createSQL UTF8String],NULL,NULL,&errorMsg)!=SQLITE_OK)

      {

      sqlite3_close(database);

      NSAssert1(0,@“Error creating table: %s”,errorMsg);

      }

      NSString *strOne = @“Persistent data1”;

      NSString *strTwo = @“Persistent data 2”;

      NSMutableArray *persistentArray = [[NSMutableArray alloc] init];

      [persistentArray addObject:strOne];

      [persistentArray addObject:strTwo];

      for (int i = 0; i 《 [persistentArray count]; i++) {

      NSString *upDataSQL = [[NSString alloc] initWithFormat:@“insert or replace into

      fields (row,field_data) values (%d,‘%@’);”,i,[persistentArray objectAtIndex:i]];

      char* errorMsg;

      if(sqlite3_exec(database,[upDataSQL UTF8String],NULL,NULL,&errorMsg)

      != SQLITE_OK)

      {

      sqlite3_close(database);

      NSAssert(0,@“Failed to open database”);

      }

      }

      //unarchive

      NSString *query = @“select row, field_data from fields order by row”;//查找表中的數(shù)據(jù)

      sqlite3_stmt *statement;

      if(sqlite3_prepare_v2(database, [query UTF8String], -1, &statement, nil)

      == SQLITE_OK)

      {

      while(sqlite3_step(statement) == SQLITE_ROW)

      {

      int row = sqlite3_column_int(statement, 0);

      char *rowData = (char *)sqlite3_column_text(statement, 1);

      NSString *fieldName = [[NSString alloc] initWithFormat:@“show%d”,row];

      NSString *fieldValue = [[NSString alloc] initWithUTF8String:rowData];

      NSLog(@“fieldName is :%@,fieldValue is :%@”,fieldName,fieldValue);

      [fieldName release];

      [fieldValue release];

      }

      sqlite3_finalize(statement);

      }

      // Override point for customization after application launch

      [window makeKeyAndVisible];

      }

      在上面的代碼中,我們使用

      NSFileManager *fileManager = [NSFileManager defaultManager];

      BOOL findFile = [fileManager fileExistsAtPath:paths];

      來(lái)判斷數(shù)據(jù)庫(kù)文件是否已經(jīng)存在,其實(shí)在大多數(shù)情況下是沒(méi)有必要的,sqlite3_open()方法會(huì)自動(dòng)幫我們判斷數(shù)據(jù)庫(kù)文件是否存在,如果不存在則創(chuàng)建心的數(shù)據(jù)庫(kù)文件。

      四、其它方法

      除了上面的三種方法來(lái)保存持久化數(shù)據(jù)以外,我們還可以用寫(xiě)文件到磁盤(pán)的方式來(lái)保存持久化數(shù)據(jù)。

      - (void)applicationDidFinishLaunching:(UIApplication *)application {

      NSString *strOne = @“Persistent data1”;

      NSString *strTwo = @“Persistent data 2”;

      NSMutableArray *persistentArray = [[NSMutableArray alloc] init];

      [persistentArray addObject:strOne];

      [persistentArray addObject:strTwo];

      NSArray *filePathArray = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

      NSString *filePath =

      - [[filePathArray objectAtIndex:0] stringByAppendingPathComponent:@“mydatas.plist”];

      [[NSArray arrayWithObjects:persistentArray,nil] writeToFile:filePath atomically:NO];

      //load

      NSMutableArray *saveDataArray = [[NSMutableArray alloc] init];

      if([[NSFileManager defaultManager] fileExistsAtPath:filePath])

      saveDataArray = [NSMutableArray arrayWithContentsOfFile:filePath];

      else

      saveDataArray = [NSMutableArray arrayWithContentsOfFile:[[NSBundle

      - mainBundle] pathForResource:@“Savedatas” ofType:@“plist”]];

      -

      NSArray *strArray = [saveDataArray objectAtIndex:0];

      NSString *UnstrOne = [strArray objectAtIndex:0];

      NSString *UnstrTwo = [strArray objectAtIndex:1];

      // Override point for customization after application launch

      [window makeKeyAndVisible];

      }

    (審核編輯: 智匯小新)

    聲明:除特別說(shuō)明之外,新聞內(nèi)容及圖片均來(lái)自網(wǎng)絡(luò)及各大主流媒體。版權(quán)歸原作者所有。如認(rèn)為內(nèi)容侵權(quán),請(qǐng)聯(lián)系我們刪除。

    主站蜘蛛池模板: 亚洲第一免费视频网站 | 羞羞视频在线免费 | 一本色道久久综合亚洲精品不卡 | 欧美怡红院视频一区二区三区 | 黄网站在线播放 | 天天天操操操 | 日韩亚洲一区二区 | 成人一区二区在线播放 | 国产成人精品亚洲日本在线观看 | 久久综合一区二区三区 | 国产区视频在线观看 | 免费在线黄色av | 久久久国产精品视频 | 亚洲 欧美 在线 一区 | 亚洲精品视频在线 | 国产99久久精品一区二区永久免费 | 欧美视频免费在线 | 国产伦在线 | 欧美福利二区 | 欧美日韩不卡合集视频 | 色网站视频 | 亚洲高清在线观看 | 日韩成人不卡 | 欧美日韩免费一区二区三区 | 日本黄色免费 | 日韩午夜一级片 | 西西做爰免费视频 | 欧亚视频在线观看 | 亚洲巨乳自拍在线视频 | 日本99精品 | 国产精品欧美日韩在线观看 | 欧美日韩精品一区二区三区 | 黄色电影在线免费观看 | 久久九九 | 久久久99精品免费观看 | 欧美成人激情视频 | 黄av片| 成人免费看电影 | 久久久精品久久久久 | 欧美日本久久 | 免费看的黄色小视频 |