IOS利用objc/runtime和KVC——快速归档解档

  开发过程中有时需要存储一些轻量级的数据,对于IOS提供的几种数据存储方式在这时最合适当属对象归档:NSCoding

  但是将对象数据进行归档解档时候需要实现两个方法: encodeWithCoder和initWithEncoder。encodeWithCoder就是编码,initWithCoder就是解码。

encodeWithCoder方法传入的是一个NSCoder对象,实现的时候我们就可以调用encodeObject、encodeFloat、 encodeInt等各种方法并通过指定键值进行编码,而

在需要的时候调用decodeDoubleForKey,decodeFloatForKey,decodeObjectForKey进行解码。

  1.实现encodeWithCoder和initWithEncoder

#import "MyModel.h"

@implementation MyModel

#define kCUSTIDKey @"CUSTIDKey"
#define kCUSTSTATUSKey @"CUSTSTATUSKey"
#define kPICTUREIDKey @"PICTUREIDKey"
#define kUNITNUMKey @"UNITNUMKey"
#define kCITYIDKey @"CITYIDKey"
#define kCUSTNAMEKey @"CUSTNAMEKey"
#define kCOMMUNITYNAMEKey @"COMMUNITYNAMEKey"
#define kRESERVE1Key @"RESERVE1Key"
#define kCITYKey @"CITYKey"
#define kCOMMUNITYIDKey @"COMMUNITYIDKey"
#define kCITYNAMEKey @"CITYNAMEKey"
#define kBUILDINGNUMKey @"BUILDINGNUMKey"
#define kNICKNAMEKey @"NICKNAMEKey"
#define kHOUSENUMKey @"HOUSENUMKey"
#define kPHONEKey @"PHONEKey"

//归档
- (void)encodeWithCoder:(NSCoder *)aCoder
{
    [aCoder encodeObject:self.CUSTID forKey:kCUSTIDKey];
    [aCoder encodeObject:self.CUSTSTATUS forKey:kCUSTSTATUSKey];
    [aCoder encodeObject:self.PICTUREID forKey:kPICTUREIDKey];
    [aCoder encodeObject:self.UNITNUM forKey:kUNITNUMKey];
    [aCoder encodeObject:self.PHONE forKey:kPHONEKey];
    [aCoder encodeObject:self.CITYID forKey:kCITYIDKey];
    [aCoder encodeObject:self.CUSTNAME forKey:kCUSTNAMEKey];
    [aCoder encodeObject:self.COMMUNITYNAME forKey:kCOMMUNITYNAMEKey];
    [aCoder encodeObject:self.RESERVE1 forKey:kRESERVE1Key];
    [aCoder encodeObject:self.CITY forKey:kCITYKey];
    [aCoder encodeObject:self.COMMUNITYID forKey:kCOMMUNITYIDKey];
    [aCoder encodeObject:self.CITYNAME forKey:kCITYNAMEKey];
    [aCoder encodeObject:self.BUILDINGNUM forKey:kBUILDINGNUMKey];
    [aCoder encodeObject:self.NICKNAME forKey:kNICKNAMEKey];
    [aCoder encodeObject:self.HOUSENUM forKey:kHOUSENUMKey];
}

// 解档
- (id)initWithCoder:(NSCoder *)decoder
{
    if (self = [super init]) {
        self.CUSTID = [decoder decodeObjectForKey:kCUSTIDKey];
        self.CUSTSTATUS = [decoder decodeObjectForKey:kCUSTSTATUSKey];
        self.PICTUREID = [decoder decodeObjectForKey:kPICTUREIDKey];
        self.PHONE = [decoder decodeObjectForKey:kPHONEKey];
        self.UNITNUM = [decoder decodeObjectForKey:kUNITNUMKey];
        self.CITYID = [decoder decodeObjectForKey:kCITYIDKey];
        self.CUSTNAME = [decoder decodeObjectForKey:kCUSTNAMEKey];
        self.COMMUNITYNAME = [decoder decodeObjectForKey:kCOMMUNITYNAMEKey];
        self.RESERVE1 = [decoder decodeObjectForKey:kRESERVE1Key];
        self.CITY = [decoder decodeObjectForKey:kCITYKey];
        self.COMMUNITYID = [decoder decodeObjectForKey:kCOMMUNITYIDKey];
        self.CITYNAME = [decoder decodeObjectForKey:kCITYNAMEKey];
        self.BUILDINGNUM = [decoder decodeObjectForKey:kBUILDINGNUMKey];
        self.NICKNAME = [decoder decodeObjectForKey:kNICKNAMEKey];
        self.HOUSENUM = [decoder decodeObjectForKey:kHOUSENUMKey];
    }
    return self;
}
@end

  写完之后发现整个人都不好了,如果对象属性更多,这些重复性的代码也意味着要被Command+C Command+V着N次,累觉无爱。

  然后尝试用其他方式进行,既然Objective-C运行时库提供了非常便利的方法获取其对象运行时所属类及其所有成员变量,并通过KVC进行值的存取,那么或者可以这样objc/runtime+KVC

  导入#import <objc/runtime.h>头文件

//获取类中所有属性
    unsigned int count;
    Ivar *varA = class_copyIvarList([MyModel class], &count);
    for (unsigned int i=0; i<count; i++) {
        Ivar v = varA[i];
        const char *name = ivar_getName(v);
        NSLog(@"%s==",name);
    }

  打印LOG:

    

  好用,继续,利用KVC进行取值,根据属性名称获取对应的值

  //KVC取值
        id value = [self valueForKey:strName];
        [encoder encodeObject:value forKey:strName];

  最终改后的程序:

#import "MyModel.h"
#import <objc/runtime.h>

@implementation MyModel

//解档
- (id)initWithCoder:(NSCoder *)decoder
{
    if (self = [super init]) {
        unsigned int count = 0;
        //获取类中所有成员变量名
        Ivar *ivar = class_copyIvarList([MyModel class], &count);
        for (int i = 0; i<count; i++) {
            Ivar iva = ivar[i];
            const char *name = ivar_getName(iva);
            NSString *strName = [NSString stringWithUTF8String:name];
            //进行解档取值
            id value = [decoder decodeObjectForKey:strName];
            //利用KVC对属性赋值
            [self setValue:value forKey:strName];
        }
        free(ivar);
    }
    return self;
}
//归档
- (void)encodeWithCoder:(NSCoder *)encoder
{
    unsigned int count;
    Ivar *ivar = class_copyIvarList([MyModel class], &count);
    for (int i=0; i<count; i++) {
        Ivar iv = ivar[i];
        const char *name = ivar_getName(iv);
        NSString *strName = [NSString stringWithUTF8String:name];
        //利用KVC取值
        id value = [self valueForKey:strName];
        [encoder encodeObject:value forKey:strName];
    }
    free(ivar);
}
@end

妥妥的了,远离重复,做开心玩耍的程序媛!

时间: 2024-12-17 04:36:25

IOS利用objc/runtime和KVC——快速归档解档的相关文章

Swift 使用Runtime对模型进行归档解档

Swift 使用Runtime对模型进行归档解档 func encode(with aCoder: NSCoder) { var count: UInt32 = 0 let propertyList = class_copyPropertyList(self.classForCoder, &count) for index in 0..<Int(count) { guard let pty = propertyList?[index], let cName = property_getNam

iOS归档,解档

iOS中,将一个实例对象存入沙盒中,叫归档;从沙盒文件中读取一个实例对象,叫解档. 下面即将诞生一个栗子:比如你想把一个ZHHPerson类中的name与age属性的值存入沙盒文件中,并在需要的时候,去沙盒文件中读取出来. 分析: 要对Person类的对象实现归档,解档的操作,必须要实现这两个方法:encodeWithCoder:归档时系统自动执行;initWithCoder:解档时系统自动执行. 对着两个方法的实现,这里采用的是高大上的用法:运行时机制.这个栗子看不出运行时机制的好处.但是的但

iOS 浅赋值、深复制、完全复制的知识点梳理验证(附加归档解档)

写于前: 在之前转载的一片文章中,文中对浅复制和深复制进行了详细的解读,同时还提到了深复制(one-level-deep copy).完全复制(true copy)的概念,并指出iOS开发中的深复制是单层深赋值,本文将对这几个概念进行验证梳理. (单层和完全概念区分:例如多层数组只实现一层内容拷贝,其他层为指针拷贝成为单层深复制:若多层内容都实现拷贝称为完全赋值) 程序中用到的几点概念补充 (1) 浅复制(shallow copy):在浅复制操作时,对于被复制对象的每一层都是指针复制. 深复制(

iOS归档解档

使用NSKeyedArichiver进行归档.NSKeyedUnarchiver进行接档,这种方式会在写入.读出数据之前对数据进行序列化.反序列化操作. 1.对单个字符串归档 //获取根目录 NSString *homeDictionary = NSHomeDirectory(); //添加储存的文件名 NSString *homePath = [homeDictionary stringByAppendingPathComponent:@"myText.txt"]; //归档一个字符

数据存储之归档解档

归档也叫序列化,是将文件存在硬盘,解码是从硬盘还原一.使用属性列表进行归档 如果对象是NSString,NSDictionary,NSArray,NSData,NSNumber,NSDate,可以是使用writeToFile:atomically方法将数据写到文件,注意这种方式是明文. NSArray *inputArray = [NSArray arrayWithObjects:@"abc", @"123", @"qiaohaibin"]; /

归档 解档

//系统类型的对象归档(NSString/NSArray/NSDictionary) //1.设置归档路径,该路径需要详细到文件(不能是文件夹) //2.得到要归档的对象 //3.通过NSKeyedArchiver调用archiveRootObject方法,进行归档 //4.解档 通过NSKeyedUnarchiver调用unarchiveObjectWithFile进行解档,注意,该方法返回值类型为id //字符串的归档 解档 NSString *path = NSHomeDirectory(

IOS开发——UI进阶篇(十一)应用沙盒,归档,解档,偏好设置,plist存储,NSData,自定义对象归档解档

1.iOS应用数据存储的常用方式XML属性列表(plist)归档Preference(偏好设置)NSKeyedArchiver归档(NSCoding)SQLite3 Core Data 2.应用沙盒每个iOS应用都有自己的应用沙盒(应用沙盒就是文件系统目录),与其他文件系统隔离.应用必须待在自己的沙盒里,其他应用不能访问该沙盒应用沙盒的文件系统目录,如下图所示(假设应用的名称叫Layer)模拟器应用沙盒的根路径在: (apple是用户名, 8.0是模拟器版本)/Users/apple/Libra

runtime学习实战一:类的属性进行归档解档

#import <Foundation/Foundation.h> @interface PYPerson : NSObject @property (nonatomic, assign) int age; @property (nonatomic, assign) int height; @property (nonatomic, copy) NSString *name; @property (nonatomic, assign) int age2; @property (nonatomi

iOS之归档解档

前几篇文章说到了OC中的Foundation框架:http://blog.csdn.net/jiangwei0910410003/article/details/41852835,今天我们来看一下OC中的一个重要知识点:归档 OC中的归档就是将对象写入到一个文件中,Java中的ObjectInputStream和ObjectOutputStream来进行操作的.当然在操作的这些对象都是需要实现一个接口:Serializable,同样的OC中操作的对象也是需要实现一个协议的,后面会说到. 一.已有