“-(void)encodeWithCoder:(NSCoder *)aCoder{
unsigned int outCount;
Ivar *ivarList = class_copyIvarList([Person class], &outCount);
for (NSInteger i = 0; i < outCount; i++) {
const char *cName = ivar_getName(ivarList[i]);
NSString *name = [NSString stringWithUTF8String:cName];
[aCoder encodeObject:[self valueForKey:name] forKey:name];
}
}
-(instancetype)initWithCoder:(NSCoder *)aDecoder
{
self = [super init];
if (self) {
unsigned int outCount;
Ivar *ivarList = class_copyIvarList([Person class], &outCount);
for (NSInteger i = 0; i < outCount; i++) {
const char *cName = ivar_getName(ivarList[i]);
NSString *name = [NSString stringWithUTF8String:cName];
[self setValue:[aDecoder decodeObjectForKey:name] forKey:name];
}
}
return self;
}
/*
Ivar *class_copyIvarList(Class cls, unsigned int *outCount) //获取所有成员变量
const char *ivar_getName(Ivar v) //获取某个成员变量的名字
const char *ivar_getTypeEncoding(Ivar v) //获取某个成员变量的类型编码
Ivar class_getInstanceVariable(Class cls, const char *name) //获取某个类中指定名称的成员变量
id object_getIvar(id obj, Ivar ivar) //获取某个对象中的某个成员变量的值
void object_setIvar(id obj, Ivar ivar, id value) //设置某个对象的某个成员变量的值
TypeEncoding:https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/ObjCRuntimeGuide/Articles/ocrtTypeEncodings.html#//apple_ref/doc/uid/TP40008048-CH100-SW1
*/
-(NSString *)description{
/** class:要获取的某个类, outCount:通过一个函数执行之后会将成员变量的数值赋值到此 */
unsigned int outCount;
Ivar *ivarList = class_copyIvarList([Person class], &outCount);
for (NSInteger i = 0; i < outCount; i++) {
//每次获取一个成员变量
Ivar ivar = ivarList[i];
//打印成员变量的名字和类型编码
NSLog(@"name = %s, type = %s", ivar_getName(ivar),ivar_getTypeEncoding(ivar));
}
return nil;
}
+(Person *)personWithName:(NSString *)name gender:(NSString *)gender age:(NSNumber *)age weight:(NSInteger)weight
{
Person *person = [Person new];
unsigned int outCount;
Ivar *ivarList = class_copyIvarList(self, &outCount);
// obj:要设置的对象 ivar:要设置的对象的某一个属性 value:value
object_setIvar(person, ivarList[0], name);
object_setIvar(person, ivarList[1], gender);
object_setIvar(person, ivarList[2], age);
object_setIvar(person, ivarList[3], @(weight));
return person;
}
-(void)getPersonMessage
{
unsigned int outCount;
Ivar *ivarList = class_copyIvarList([Person class], &outCount);
for (NSInteger i = 0; i < outCount; i++) {
NSLog(@"name = %s, value = %@", ivar_getName(ivarList[i]), object_getIvar(self, ivarList[i]));
}
}
/*
1 + resolveInstanceMethod:(SEL)sel // 为一个实例方法动态添加实现
+ resolveClassMethod:(SEL)sel // 为一个类方法动态添加实现
2 - (id)forwardingTargetForSelector:(SEL)aSelector
//为没有实现的方法指定一个对象
3 - (void)forwardInvocation:(NSInvocation *)anInvocation
//子类重载这个方法为消息指定其他对象
*/
//消息转化1:为一个实例方法动态添加实现
+ (BOOL)resolveInstanceMethod:(SEL)sel{
NSString *selString = NSStringFromSelector(sel);
if ([selString isEqualToString:@"wolkOnTheStreet:"]) {
// 为一个没有实现的方法添加实现
/** cls:类
name:没有实现的方法
IMP:要添加的方法
types:动态添加的实现的类型编码
*/
class_addMethod(self, @selector(wolkOnTheStreet:), (IMP)wolkFunc, "V@:@");
}
return [super resolveInstanceMethod:sel];
}
void wolkFunc(id self,SEL sel, NSString *str){
NSLog(@"Person---%s---%@", __func__,str);
}
- (id)forwardingTargetForSelector:(SEL)aSelector
{
NSString *selString = NSStringFromSelector(aSelector);
if ([selString isEqualToString:@"wolkOnTheStreet:"]) {
self.dog = [Dog new];
return self.dog;
}
return [super forwardingTargetForSelector:aSelector];
}
//切换消息转化对象:二
- (void)forwardInvocation:(NSInvocation *)anInvocation
{
if ([Dog instancesRespondToSelector:anInvocation.selector]) {
self.dog = [Dog new];
[anInvocation invokeWithTarget:self.dog];
}
}
//给方法制定一个有效的签名
-(NSMethodSignature *)methodSignatureForSelector:(SEL)aSelector
{
NSMethodSignature *methodSignature = [super methodSignatureForSelector:aSelector];
if (!methodSignature) {
methodSignature = [Dog instanceMethodSignatureForSelector:aSelector];
}
return methodSignature;
}”
网友评论