iOS開(kāi)發(fā)之?dāng)?shù)據(jù)存儲(chǔ)

一、數(shù)據(jù)存儲(chǔ)的幾種方式

  1. NSUserDefaults
  2. Plist文件
  3. 解歸檔
  4. 沙盒
  5. SQLite
  6. CoreData
1.NSUserDefaults
  • 適用:適用于數(shù)據(jù)量小的,比如一些基本的用戶設(shè)置。
  • 存儲(chǔ)類型:NSArray、NSDictionary、NSData、OC基本類型屬性
  • 不可存儲(chǔ):不可直接存儲(chǔ)自定義的對(duì)象(自定義的需要進(jìn)行歸檔再存儲(chǔ))
  • 使用方法:
// 1.創(chuàng)建數(shù)組
NSMutableArray *arr = [[NSMutableArray alloc] initWithObjects:@"1", nil];
// 2.存入NSUserDefaults
[[NSUserDefaults standardUserDefaults] setObject:arr forKey:@"ArrKey"];
// 3.同步
[[NSUserDefaults standardUserDefaults] synchronize];
// 4.讀取NSUserDefaults的數(shù)據(jù)
NSArray *arr = [[NSUserDefaults standardUserDefaults] objectForKey:@"ArrKey"];
2.Plist文件-Property List(是Xcode的一種資源包,同樣是一種存儲(chǔ)工具)
  • 優(yōu)缺點(diǎn):優(yōu)點(diǎn)是可視化很直觀的看到文件內(nèi)容而且便于直接操作文件,缺點(diǎn)是因?yàn)槠湟话阕饔糜诠虘B(tài)的數(shù)據(jù)形式保存而經(jīng)常變動(dòng)的數(shù)據(jù)不好操作了
  • 適用:適用于固態(tài)的數(shù)據(jù)存儲(chǔ)
  • 存儲(chǔ)類型:NSArray、NSDictionary、NSData、Boolean、NSDate、NSNumber、NSString
  • 不可存儲(chǔ):不可直接存儲(chǔ)自定義的對(duì)象(自定義的需要進(jìn)行歸檔再存儲(chǔ))
  • 使用方法:
方法1:
// 1.創(chuàng)建Property List文件
// 2.設(shè)置對(duì)應(yīng)的值
// 3.獲取文件中的數(shù)據(jù)
NSString *str = [[NSBundle mainBundle] pathForResource:@"testPlistFile" ofType:@"plist"];
// 4.獲取內(nèi)容
NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile:str];

方法2:
// 1.獲取用戶主目錄
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
// 2. 取出路徑
NSString *plistPath = [paths objectAtIndex:0];
// 3.新建Plist文件路徑
NSString *fileName = [plistPath stringByAppendingPathComponent:@"testPlist.plist"];
// 4.創(chuàng)建Plist文件
NSFileManager *fm = [NSFileManager defaultManager];
[fm createFileAtPath:fileName contents:nil attributes:nil];
// 5.創(chuàng)建數(shù)據(jù)
NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:@"test", nil];
// 6.存入Plist文件
[dict writeToFile:fileName atomically:YES];
// 7.讀取Plist文件
NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile:fileName];
3.解歸檔
  • 優(yōu)缺點(diǎn):優(yōu)點(diǎn)是解決了不能將自定義對(duì)象存儲(chǔ)到NSUserDefault或Plist文件,解歸檔針對(duì)的就是一個(gè)對(duì)象
  • 適用:
  • 存儲(chǔ)類型:NSArray、NSDictionary、NSData、Boolean、NSDate、NSNumber、NSString
  • 不可存儲(chǔ):
  • 使用方法:
// 1.首先我們擁有一個(gè)自定義對(duì)象XHModel,要遵循NSCoding協(xié)議并實(shí)現(xiàn)協(xié)議方法
#import <Foundation/Foundation.h>
@interface XHModel:NSObject <NSCoding> 
@property (nonatomic, strong) NSString *name;
@property (nonatomic, assign) NSInteger age;
@end

#import "XHModel.h"
@implementation XHModel
- (id)initWithCode:(NSCode *)aDecoder
{
    if (self = [super init]) {
        self.name = [aDecoder decodeObjectForKey:@"name"];
        self.age = [[aDecoder decodeObjectForKey:@"age"] integerValue];
    }
    return self;
}

- (void)encodeWithCoder:(NSCoder *)aCoder
{
    [aCoder encodeObject:self.name forKey:@"name"];
    [aCoder encodeObject:[NSNumber numberWithInteger:self.age] forKey:@"age"];
}

- (NSString *)description
{
    return [NSString stringWithFormat:@"%@--%ld歲", self.name, (long)self.age];
}

// 2.外部創(chuàng)建模型
XHModel *model = [[XHModel alloc] init];
model.name = @"小黃";
model.age = 18;
// 3.創(chuàng)建NSData
NSMutableData *data = [[NSMutableData alloc] init];
// 4.創(chuàng)建歸檔輔助類
NSKeyedArchiver *archiver = [[NSKeyedArchiver alloc] initForWritingWithMutableData:data];
// 5.編碼
[archiver encodeObject:model forKey:@"model"];
[archiver finishEncoding];
// 6.寫(xiě)入到沙盒
NSArray *array = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,  NSUserDomainMask, YES);
NSString *fileName = [array.firstObject stringByAppendingPathComponent:@"archiverModel"];
if([data writeToFile:fileName atomically:YES]){
    NSLog(@"歸檔成功");
}
    
//解檔
NSData *undata = [[NSData alloc] initWithContentsOfFile:fileName];
//解檔輔助類
NSKeyedUnarchiver *unarchiver = [[NSKeyedUnarchiver alloc] initForReadingWithData:undata];
//解碼并解檔出model
TestModel *unModel = [unarchiver decodeObjectForKey:@"model"];
NSLog(@"%@",unModel);
//關(guān)閉解檔
[unarchiver finishDecoding];
4.存放沙盒
  • Documents目錄:存放應(yīng)用程序所有數(shù)據(jù)文件,該目錄用于倉(cāng)儲(chǔ)用戶數(shù)據(jù)或者其他應(yīng)該定期備份的信息,為了不讓App備份過(guò)于龐大,不建議存放大容量文件
  • App目錄:
  • Library目錄:
  • Tmp目錄:
  • 優(yōu)缺點(diǎn):優(yōu)點(diǎn)是
  • 適用:
  • 存儲(chǔ)類型:NSArray、NSDictionary、NSData、Boolean、NSDate、NSNumber、NSString
  • 不可存儲(chǔ):
  • 使用方法:
######5.NSUserDefaults
######6.NSUserDefaults
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容