iOS快速歸檔存儲自定義對象

前言

前幾天做項目,需要保存自定義的對象,也就是數(shù)據(jù)Model,之前也在這篇文章里講到過存儲自定義對象的方法,但是那種方法感覺太麻煩了,需要每次歸檔model的每一個屬性,如果需要歸檔的model不多,那這種方法還是ok的,如果項目中需要歸檔的model比較多的話感覺會很麻煩,所以就想了個工具類來快速實現(xiàn)歸檔自定義對象

快速歸檔

  • 這個方法是在學(xué)習(xí)MJExtension源碼的時候想到的,其實MJExtension已經(jīng)實現(xiàn)了這種功能,
  • 這個工具類用到的知識點很簡單, runtimekvc而已

這個工具類很簡單,目錄如下

C087F921-07B7-47E2-BC08-5AA279F61DA9.png
使用方法

在自定義的Model的.m文件引入#import "NSObject+WYCoding.h"文件,然后書寫WYCodingImplementation宏就ok了,如下圖所示

160C9D21-93EF-4B1C-8053-03A530A4C63D.png

這里提供了三個方法來自定義歸檔的屬性

  • + (NSArray *)wy_allowedCodingPropertyNames;允許歸檔的屬性數(shù)組
  • + (NSArray *)wy_ignoredCodingPropertyNames;忽略歸檔的屬性數(shù)組
  • + (BOOL)wy_ignoreCodingSuperClassPropertyNames;是否忽略歸檔父類的屬性

歸檔Model

這里自定義了一個Person類,定義了不同類型的屬性如下


@property (nonatomic , copy) NSString *name;
@property (nonatomic , assign) NSInteger age;
@property (nonatomic , assign) long longValue;
@property (nonatomic , assign) double doub;
@property (nonatomic , assign) NSUInteger uint;
@property (nonatomic , strong) NSArray *titles;
@property (nonatomic , strong) Student *stu;
@property (nonatomic , copy) NSDictionary *data;
@property (nonatomic , assign) CGFloat floatNumber;

開始歸檔Person



- (IBAction)archPerson:(id)sender {
    Person *person = [[Person alloc] init];
    person.name = @"personName";
    person.age = 100;
    person.titles = @[@"jack",@"mattt"];
    person.data = @{@"list":@[@{@"id":@"187",
                                @"time":@"2017.4.21"}],
                    @"success":@"ture"};
    person.longValue = 69;
    person.doub = 666.66;
    person.uint =  90;
    person.floatNumber = 10.55;
    
    Student *stu = [[Student alloc] init];
    stu.name = @"person的學(xué)生";
    stu.age = 99;
    
    person.stu = stu;
    
    NSString *filePath = [[WYFileManager getDocumentFilePath] stringByAppendingPathComponent:@"person"];
    //保存
    [WYFileManager setCustomObject:person forKey:@"person" filePath:filePath];
    
    
    _currentKey = @"person";
    _currentModelButton = sender;
}

解檔Person


- (IBAction)unarchPerson:(id)sender {
    
    Person *person = [WYFileManager getCustomObjectWithKey:@"person"];
    
    
    NSLog(@"person --- name: %@  age: %ld longValue: %ld doub: %ld uint: %lu floatNumber: %.2f   student`s properties name: %@ age: %ld    ", person.name , (long)person.age, person.longValue, (long)person.doub , (unsigned long)person.uint,person.floatNumber, person.stu.name, person.stu.age);
    
    
    NSDictionary *data = person.data;
    
    NSLog(@"字典  ---  %@", data);
    
    for (NSString *title in person.titles) {
        
        NSLog(@"數(shù)組· %@",title);
    }
}

打印信息為

2017-05-01 19:25:02.600 WYCoding[10163:2327318] person --- name: personName  age: 100 longValue: 69 doub: 666 uint: 90  10.55   student`s properties name: person的學(xué)生 age: 99    
2017-05-01 19:25:02.600 WYCoding[10163:2327318] 字典  ---  {
    list =     (
                {
            id = 187;
            time = "2017.4.21";
        }
    );
    success = ture;
}
2017-05-01 19:25:02.601 WYCoding[10163:2327318] 數(shù)組· jack
2017-05-01 19:25:02.601 WYCoding[10163:2327318] 數(shù)組· mattt
  • 其中WYFileManager文件是我自己寫的存儲Model至沙盒文件下的工具類,可以自己實現(xiàn)
最后

因為這個工具是學(xué)習(xí) MJExtension實現(xiàn)的,如果項目中有用到MJExtension框架的話可以直接使用MJ框架中的方法

MJ的使用

只需要在自定義model的.m文件中書寫MJExtensionCodingImplementation宏,就可以了

#import "MJExtension.h"
@implementation Person
// 宏實現(xiàn)
MJExtensionCodingImplementation
@end

歸檔Person

- (IBAction)archiver:(id)sender {
    Person *person2 = [[Person alloc] init];
    person2.name = @"dev";
    person2.age = 28;
    person2.titles = @[@"Tom",@"Jerry"];
    
    
    [WYFileManager setCustomObject:person2 key:@"person2"];
}

解檔Person

- (IBAction)unarchiver:(id)sender {
    Person *person2 = [WYFileManager getCustomObjectWithKey:@"person2"];
    
    NSLog(@"name %@  age %ld ", person2.name   , (long)person2.age);
    
    for (NSString *title in person2.titles) {
        
        NSLog(@"%@",title);
    }
}

打印信息為


2017-05-01 19:53:35.206 testMJExtension[10437:2353875] name dev  age 28 
2017-05-01 19:53:35.206 testMJExtension[10437:2353875] Tom
2017-05-01 19:53:35.207 testMJExtension[10437:2353875] Jerry

代碼下載地址:Github下載

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

推薦閱讀更多精彩內(nèi)容