iOS中的數據存儲

  • 應用沙盒
  • 歸檔
  • 偏好設置
  • Plist

應用沙盒

Documents
Library -- Caches
        -- Preferences
tmp

歸檔

Person.h

#import <Foundation/Foundation.h>

@interface Person : NSObject <NSCoding>

/** 姓名 */
@property(nonatomic,copy) NSString* name;
/** 昵稱 */
@property(nonatomic,copy) NSString* nickName;

@end

Person.m

#import "Person.h"

@implementation Person

// 歸檔:只要一個自定義對象歸檔的時候就會調用,告訴系統(tǒng) 哪些屬性需要歸檔
- (void)encodeWithCoder:(NSCoder *)coder
{
    [coder encodeObject:_name forKey:@"name"]; // 給屬性設置一個key
    [coder encodeObject:_nickName forKey:@"nickName"];
}


//解檔:只要一個自定義對象解檔的時候就會調用,告訴系統(tǒng) 解析key得到的值,賦值給哪個屬性
- (id)initWithCoder:(NSCoder *)aDecoder{
//    什么時候調用 [super initWithCoder]
    if (self = [super init]) {
        _name = [aDecoder decodeObjectForKey:@"name"];
        _nickName = [aDecoder decodeObjectForKey:@"nickName"];
    }
    return self;
}

@end

ViewController.m

#import "ViewController.h"
#import "Person.h"
//---------------思路----------------
//1、創(chuàng)建自定義對象
//   重寫encodeWithCoder 歸檔
//   initWithCoder方法  解檔
//2、創(chuàng)建路徑  /caches
//3、使用歸檔類方法進行存儲
//   使用解檔類方法進行解檔
//-----------------------------------

@interface ViewController ()

- (IBAction)save:(id)sender;
- (IBAction)read:(id)sender;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
}

- (IBAction)save:(id)sender {
//    1、創(chuàng)建對象
    Person *p = [[Person alloc] init];
    p.name = @"李明";
    p.nickName = @"hahaha";
    
//    2.1、創(chuàng)建路徑/tmp,使用函數
    NSString *cachesPath = NSTemporaryDirectory();
//    2.2、拼接路徑;擴展名隨意寫,蘋果對內容進行了加密
    NSString *path = [cachesPath stringByAppendingString:@"person.data"];
    
//    3、使用歸檔類方法進行存儲
    [NSKeyedArchiver archiveRootObject:p toFile:path];
}

- (IBAction)read:(id)sender {
    
    NSString *cachesPath = NSTemporaryDirectory();
    NSString *path = [cachesPath stringByAppendingString:@"person.data"];
    
    Person *p = [NSKeyedUnarchiver unarchiveObjectWithFile:path];
    NSLog(@"%@,%@",p.name,p.nickName);
}
@end

歸檔:什么時候調用super initWithCoder

#import "MyView.h"
@implementation MyView

// 解析文件的時候調用
// 作用:解析xib,storyboard調用
// 可以猜測 我們平時在storyboard中拖動的控件,是通過歸檔存儲的,當創(chuàng)建的時候就解析 歸檔文件 然后實例化
- (id)initWithCoder:(NSCoder *)aDecoder{
    // 這里必須調用[super initWithCoder:aDecoder],super ->UIView
    // 什么時候調用[super initWithCoder:aDecoder],只要父類遵守了NSCoding協(xié)議,就調用[super initWithCoder:aDecoder]
    if(self = [super initWithCoder:aDecoder])
    {
        NSLog(@"調用了initWithCoder");
    }
    return self;
}

@end

偏好設置

#import "ViewController.h"

@interface ViewController ()

- (IBAction)save:(id)sender;
- (IBAction)read:(id)sender;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
}

- (IBAction)save:(id)sender {
//    /Library/Preferences
//    獲取單例對象
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    [userDefaults setValue:@"YChiong" forKey:@"name"];
    [userDefaults setObject:@"xmg" forKey:@"address"];
    [userDefaults setBool:YES forKey:@"isHandSome"];
    [userDefaults setInteger:25 forKey:@"age"];
}

- (IBAction)read:(id)sender {
    //    獲取單例對象
    NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
    
    NSString *name = [userDefaults valueForKey:@"name"];
    NSString *address = [userDefaults objectForKey:@"address"];
    BOOL isHandSome = [userDefaults boolForKey:@"isHandSome"];
    NSInteger age = [userDefaults integerForKey:@"age"];
    
    NSLog(@"%@,%@,%i,%i",name,address,isHandSome,age);
}
@end

Plist

#import "ViewController.h"

@interface ViewController ()
- (IBAction)save:(id)sender;
- (IBAction)read:(id)sender;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
}

- (IBAction)save:(id)sender {
//    1、創(chuàng)建數組或者字典
    NSArray *arr = @[@"YChiong",@25];
//    2、創(chuàng)建存儲路徑
//    2.1  /Library/Caches文件夾
//    參數:
//    NSSearchPathDirectory directory:獲取哪個文件夾
//    NSSearchPathDomainMask domainMask:在哪個范圍下獲取 NSUserDomainMask:在用戶的范圍內搜索?
//    BOOL expandTilde:expandTilde是否展開全路徑,YES:展開
//    返回值:一個數組
    NSString *cachePath = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)[0];
//    2.2 拼接路徑
    NSString *plistPath = [cachePath stringByAppendingPathComponent:@"YChiong.plist"];
    
//    3、存儲數組/字典 到 指定路徑
    [arr writeToFile:plistPath atomically:YES];
}

- (IBAction)read:(id)sender {
//    怎么存,就怎么取
    NSString *cachePath = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES)[0];
    NSString *plistPath = [cachePath stringByAppendingPathComponent:@"YChiong.plist"];
    
    NSArray *arr = [NSArray arrayWithContentsOfFile:plistPath];
    NSLog(@"%@",arr);
}
@end
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
【社區(qū)內容提示】社區(qū)部分內容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發(fā)布,文章內容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內容

友情鏈接更多精彩內容