runtime的应用(1)
前面对runtime有了一个简单了解,可看了半天在应用在没什么用啊,那你就错了
啊,我想起来了,你前面不就要说应用吗,为毛等到现在,我还傻傻看完前一页,流量啊,其实我是来替网通做任务的
还记得归档解档吗,想想就心碎的不行啊,那一大堆的=啊,有了runtime马上心就热乎了
首先遵循NSCoding协议
//归档- (void)encodeWithCoder:(NSCoder *)aCoder{ unsigned int count; //获得指向当前类的所有属性的指针 objc_property_t *propertys = class_copyPropertyList([Student class], &count); for (int i = 0; i < count; i++) { //获得指向属性的指针 objc_property_t property = propertys[i]; const char *name = property_getName(property); NSString *properyName = [NSString stringWithUTF8String:name]; //通过名称取值 NSString *propertyValue = [self valueForKey:properyName]; //编码属性 [aCoder encodeObject:propertyValue forKey:properyName]; } free(propertys);}//解档- (instancetype)initWithCoder:(NSCoder *)aDecoder{ unsigned int count; //获得指向当前类的所有属性的指针 objc_property_t *propertys = class_copyPropertyList([Student class], &count); for (int i = 0; i < count; i++) { //获得指向属性的指针 objc_property_t property = propertys[i]; const char *name = property_getName(property); NSString *properyName = [NSString stringWithUTF8String:name]; //解码属性值 NSString *propertyValue = [aDecoder decodeObjectForKey:properyName]; [self setValue:propertyValue forKey:properyName]; } free(propertys); return self;}