ios關於資料的儲存

來源:互聯網
上載者:User

標籤:

本文轉載至http://blog.csdn.net/chen505358119/article/details/9278539            這裡我總結了一下資料的儲存方式,一是儲存在沙箱裡建立檔案,再把資料寫到檔案中,二是儲存到plist中,三是儲存到資料庫中.

          1.儲存到檔案中需要藉助NSKeyedArchiver和NSKeyedUnArchiver

          儲存資料:                 NSArray*paths=NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,               NSUserDomainMask,YES);          

 

            NSString *path = [pathsobjectAtIndex:0];

            NSMutableData *data =[[NSMutableData alloc] init];

            NSKeyedArchiver*archiver = [[NSKeyedArchiver alloc]

                                    initForWritingWithMutableData:data];

           [archiver encodeObject:_filleText.text forKey:@"ty"];

           [archiver finishEncoding];

           [data writeToFile:[path stringByAppendingPathComponent:@"test"]

               atomically:YES];

           [archiver release];

           [data release];

 

           擷取資料:

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

     NSString *path = [pathsobjectAtIndex:0];

     NSMutableData *data =[[NSMutableData alloc] initWithContentsOfFile:

                                  [path stringByAppendingPathComponent:@"test"]];

     NSKeyedUnarchiver *unarchiver =[[NSKeyedUnarchiver alloc]

                                               initForReadingWithData:data];

     NSObject*ret = [[unarchiverdecodeObjectForKey:@"ty"] retain];

     [unarchiver finishDecoding];

     [unarchiver release];

     [data release];

               也可以寫成這樣只不過前一種進行了序列化:

        儲存:   

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

 

        NSString *path = [pathsobjectAtIndex:0];

        [NSKeyedArchiver archiveRootObject:_filleText.text toFile:[pathstringByAppendingPathComponent:@"test"]];

                  擷取:    

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

     NSString *path = [pathsobjectAtIndex:0];

        NSString* str=[NSKeyedUnarchiver unarchiveObjectWithFile:[pathstringByAppendingPathComponent:@"test"]];

 

       2.儲存到plist檔案中

        分為兩種一種是儲存在系統內建的plist中,另一種是儲存在自已建立的plist檔案中

        儲存到系統內建的plist

       儲存:

 

        [[NSUserDefaults standardUserDefaults] setObject:_filleText.textforKey:@"save"];

        [[NSUserDefaults standardUserDefaults]synchronize];

     擷取資料:

 

      NSString*str=[[NSUserDefaults standardUserDefaults] objectForKey:@"save"];

          儲存到自已創的plist中

       儲存:

 

       NSMutableDictionary* dict=[NSMutableDictionarydictionaryWithObjectsAndKeys:@"chen",@"sex",@"liang",@"name",nil];

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

       NSString *docPath = [pathsobjectAtIndex:0];

       NSString *myFile = [docPath stringByAppendingPathComponent:@"user.plist"];

       [dict writeToFile:myFile atomically:YES];

              擷取資料:  

 

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

      NSString *docPath = [paths objectAtIndex:0];

      NSString *myFile = [docPath stringByAppendingPathComponent:@"user.plist"];

      NSDictionary* dic = [NSDictionary dictionaryWithContentsOfFile:myFile];

    不過我寫著寫著,覺得第一種和第二種都差不多,基本上都可以歸類為儲存資料庫到檔案中,不同的就是儲存到plist中可以開啟看到裡面的資料,第一種看不到,所以更安全些。

 3.儲存資料到資料庫裡,一般分為兩種一種是ios內建的資料庫coredata,另一種是sqllite,我想說的是因為sqllite好多和c關聯,如果對c不是很熟悉的話,建議用FMDataBase這個第三方庫,它是對sqllite的封裝,很好用,對於資料庫這塊下次再寫

 

有什麼問題請大家多指教

ios關於資料的儲存

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.