初級數據持久化

1.App文件用來存儲,你創建工程或開發時顯示在左側的所有內容。
在開發過程中可修改Bundle包得內容,在執行期間不容許修任何內容。
2、Library:存放緩存
3、tmp:臨時性存儲
4、Document:存儲用戶文件的首選目錄

-Application - Name --這個目錄是你的應用程序包,包括了nib文件,本地化資源,可以執行代碼以及別的資源。


一:沙盒機制

1、每個應用程序位於文件系統的嚴格限制部分

2、每個應用程序只能在爲該程序創建的文件系統中讀取文件

3、每個應用程序在iOS系統內都放在了統一的文件夾目錄下

二:沙盒路徑位置

1、通過Finder查找程序沙盒相對路徑

~/Library/Application Support/iPhone Simulator

2、通過代碼查找沙盒相對路徑

NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory directory,NSSearchPathDomainMask domainMask,BOOL expandTilde);

三:對象歸檔的基本概念和用法

1、概念:對象回檔是指將對象寫入文件保存到硬盤上,當再次打開程序時,可以還原這些對象。你可以將稱它爲序列化、對象持久化。

2、數據持久性的方式

1)NSKeyedArchive ——————————對象歸檔

2)NSUserDefaults

3)屬性列表化(NSArray、NSDictionary保存文件)

4)SQLite數據庫、Core Data數據庫

3、歸檔的形式()

1)對Foundation庫中的對象進行歸檔

2)自定義對象歸檔(需要實現歸檔協議,NSCoding)

4、歸檔後的文件是加密的,屬性列表是明文的。

下面我們實現一個model的歸檔和解檔,創建一個Student類,必須遵循NSCoding協議,並且初始化方法,,便利構造器都寫好。

Student.h文件:

#import <Foundation/Foundation.h>

@interface Student : NSObject<NSCoding,NSCopying>
@property (nonatomic,retain) NSString *name;
@property (nonatomic,retain) NSString *sex;
@property (nonatomic,assign) NSUInteger age;
+ (id)studentWithName:(NSString*)name sex:(NSString *)sex age:(NSUInteger)age;
@end

Student.m文件

#import "Student.h"

@implementation Student

- (void)dealloc{
    [_name release];
    [_sex release];
    _name = nil;
    _sex = nil;
    [super dealloc];
}

- (id)initWithName:(NSString*)name sex:(NSString *)sex age:(NSUInteger)age{
    if (self = [super init]) {
        _name = name;
        _sex = sex;
        _age = age;
    }
    return self;
}

+ (id)studentWithName:(NSString*)name sex:(NSString *)sex age:(NSUInteger)age{
    Student *stu = [[Student alloc] initWithName:name sex:sex age:age];
    return stu;
}
//歸檔協議或者稱爲序列化協議
- (void)encodeWithCoder:(NSCoder *)aCoder{
    [aCoder encodeObject:self.name forKey:@"name"];
    [aCoder encodeObject:self.sex forKey:@"sex"];
    NSNumber *num= [NSNumber numberWithInt:self.age];
    [aCoder encodeObject:num forKey:@"age"];
}
//解檔,反序列化
- (id)initWithCoder:(NSCoder *)aDecoder{
    if (self = [super init]) {
        self.name = [aDecoder decodeObjectForKey:@"name"];
        self.sex = [aDecoder decodeObjectForKey:@"sex"];
        NSNumber *num = [aDecoder decodeObjectForKey:@"age"];
        self.age = [num intValue];
    }
    return self;
}
@end
歸檔:在主控制器裏RootViewController.m裏引入Student.h頭文件

#import "RootViewController.h"
#import "Student.h"

@interface RootViewController ()

@end


#pragma mark archiveObject歸檔
//即將自己創建的model類型轉化爲二進制格式,通過二進制格式進行存儲
- (void)archiveObject{

    Student *stu = [[Student alloc] init];
    stu.name = @"理想";
    stu.sex = @"女";
    stu.age = 38;
    //NSArray *arr = [NSArray arrayWithObject:stu];
    NSData *data =  [NSKeyedArchiver archivedDataWithRootObject:stu];

    NSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *str = [path firstObject];

    str = [NSString stringWithFormat:@"%@/model",str];

    [data writeToFile:str atomically:YES];
    [stu release];
}
解檔:

在主控制器裏RootViewController.m裏引入Student.h頭文件

#import "RootViewController.h"
#import "Student.h"

@interface RootViewController ()

@end


#pragma mark unarchiveObject
//解檔,將歸檔後的二進制文件解檔爲相對應的model對象
- (void) unarchiveObject{

    NSArray *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *str = [path firstObject];

    str = [NSString stringWithFormat:@"%@/model",str];

    NSData *data = [NSData dataWithContentsOfFile:str];
    Student *model =  [NSKeyedUnarchiver unarchiveObjectWithData:data];
    NSLog(@"%@",model.name);
}

viewDidLoad

在viewDidload裏面調用歸檔和解檔方法。
- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    //[self searchDocument];
    //[self writeStr];
    //[self readArrayAndDictionary];
    //[self createArrayAndDictionary];
//    [self writeData];
//    [self readData];
//    [self userDefaultsWrite];
//    [self readDefautlts];
    [self archiveObject];
    [self unarchiveObject];
}



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章