美文网首页
9.26 如何在OC中使用C语言写的方法来归档解档

9.26 如何在OC中使用C语言写的方法来归档解档

作者: jayck | 来源:发表于2016-09-26 20:59 被阅读54次

问题:在Dog.m里面实现归档,解归档

已知有个C语言写的demoFunc,能拿到key,也可以转换成ocKey,现在怎么做归档和解归档?

新建Dog类,在头文件里面定义几个变量,和一个方法

Paste_Image.png

我们在Dog.m里面实现

#import "Dog.h"
#import <objc/objc-runtime.h>

@interface Dog ()<NSCoding>
{
    NSInteger xxx;
}
@end
@implementation Dog
#if 1
- (instancetype)initWithCoder:(NSCoder *)aDecoder{

    self = [super init];
    if (self) {        
        unsigned int count = 0;
        //Ivar代表C语言的数组
        Ivar *ivars = class_copyIvarList([self class], &count); 
        for (int i = 0; i<count; i++) {

            Ivar ivar = ivars[i];
            const char *key = ivar_getName(ivar);
            //把C语言写的东西转换成UTF-8编码;
            NSString *ocKey = [NSString stringWithCString:key encoding:NSUTF8StringEncoding];
            //id定义value为任意OC对象.把ocKey对应的value赋值给它.
            id value = [aDecoder decodeObjectForKey:ocKey];
            //用KVC去存数据;
            [self setValue:value forKey:ocKey];

        }
      
    }
    return nil;
}

- (void)encodeWithCoder:(NSCoder *)aCoder{
   
 unsigned int count = 0;
    //Ivar代表C语言的数组
    Ivar *ivars = class_copyIvarList([self class], &count);
//    for循环遍历
    for (int i = 0; i<count; i++) {
        
        Ivar ivar = ivars[i];
        //拿到值,赋值给key
        const char *key = ivar_getName(ivar);
        //转成oc可以读的Key
        NSString *ocKey = [NSString stringWithCString:key encoding:NSUTF8StringEncoding];
        
        //value等于ocKey对应的value
        id value = [self valueForKey:ocKey];
        //用KVC去取数据
        [aCoder encodeObject:value forKey:ocKey];
        
        NSLog(@"%@:%@",ocKey,value);
    }


//    class_copyPropertyList(<#__unsafe_unretained Class cls#>, <#unsigned int *outCount#>)
    
}
#endif
//demoFunc为之前给的示例代码
- (void)demoFunc{
    
    unsigned int count = 0;
    Ivar *ivars = class_copyIvarList([self class], &count);
    for (int i = 0; i<count; i++) {
        
        Ivar ivar = ivars[i];
        const char *key = ivar_getName(ivar);
        
        NSString *ocKey = [NSString stringWithCString:key encoding:NSUTF8StringEncoding];
        
        id value = [self valueForKey:ocKey];
        NSLog(@"%@:%@",ocKey,value);
    }   
}
@end

写法二:

也可以将老师的方法封装成block, 写法如下:
#if 1
- (instancetype)initWithCoder:(NSCoder *)aDecoder{
    
    self = [super init];
    if (self) {
        [self demoBlock:^(NSString *zzz) {
            id value = [aDecoder decodeObjectForKey:zzz];
            [self setValue:value forKey:zzz];
        }];
    }
    return self;
}

- (void)encodeWithCoder:(NSCoder *)aCoder{
    
    [self demoBlock:^(NSString *zzz) {
    id value = [self valueForKey:zzz];
    [aCoder encodeObject:value forKey:zzz];
    }];
}

-(void)demoBlock:(void(^)(NSString*zzz))blc {    
    //直接复制之前C语言编写的可以重用的代码
    unsigned int count = 0;
    Ivar *ivars = class_copyIvarList([self class], &count);
    for (int i = 0; i<count; i++) {        
        Ivar ivar = ivars[i];
        const char *key = ivar_getName(ivar);
        NSString *ocKey = [NSString stringWithCString:key encoding:NSUTF8StringEncoding];
        blc(ocKey);
    }
}
#endif

相关文章

网友评论

      本文标题:9.26 如何在OC中使用C语言写的方法来归档解档

      本文链接:https://www.haomeiwen.com/subject/cagqyttx.html