ios高级开发,runtime(三)
11、系统类的方法实现部分替换
- (void) methodExchange {
Method m1 = class_getInstanceMethod([NSString class], @selector(lowercaseString));
Method m2 = class_getInstanceMethod([NSString class], @selector(uppercaseString));
method_exchangeImplementations(m1, m2);
NSLog(@"%@",[@"aaAAAAaa" lowercaseString]);
NSLog(@"%@",[@"aaAAAAaa" uppercaseString]);
}
运行结果:
2014-10-20 14:48:17.584 runtimetest[14921:303] AAAAAAAA
2014-10-20 14:48:17.586 runtimetest[14921:303] aaaaaaaa
12、自定义类的方法实现部分替换
- (void) justLog1 {
NSLog(@"justLog1");
}
- (void) justLog2 {
NSLog(@"justLog2");
}
- (void) methodSetImplementation {
Method method = class_getInstanceMethod([ClassMethodViewCtrclass],@selector(justLog1));
IMP originalImp = method_getImplementation(method);
Method m1 = class_getInstanceMethod([ClassMethodViewCtrclass],@selector(justLog2));
method_setImplementation(m1, originalImp);
}
//[self methodSetImplementation];
//[self justLog2];
13、覆盖系统方法
IMP cFuncPointer;
IMP cFuncPointer1;
IMP cFuncPointer2;
NSString* CustomUppercaseString(idself,SEL_cmd){
printf("真正起作用的是本函数CustomUppercaseString\r\n");
NSString *string = cFuncPointer(self,_cmd);
return string;
}
NSArray* CustomComponentsSeparatedByString(idself,SEL_cmd,NSString *str){
printf("真正起作用的是本函数CustomIsEqualToString\r\n");
return cFuncPointer1(self,_cmd, str);
}
//不起作用,求解释
bool CustomIsEqualToString(idself,SEL_cmd,NSString *str) {
printf("真正起作用的是本函数CustomIsEqualToString\r\n");
return cFuncPointer2(self,_cmd, str);
}
- (void) replaceMethod{
cFuncPointer = [NSStringinstanceMethodForSelector:@selector(uppercaseString)];
class_replaceMethod([NSStringclass],@selector(uppercaseString), (IMP)CustomUppercaseString,"@@:");
cFuncPointer1 = [NSStringinstanceMethodForSelector:@selector(componentsSeparatedByString:)];
class_replaceMethod([NSStringclass],@selector(componentsSeparatedByString:), (IMP)CustomComponentsSeparatedByString,"@@:@");
cFuncPointer2 = [NSStringinstanceMethodForSelector:@selector(isEqualToString:)];
class_replaceMethod([NSStringclass],@selector(isEqualToString:), (IMP)CustomIsEqualToString,"B@:@");
}
14、自动序列化(转)
#import "NSObject+AutoEncodeDecode.h"
@implementation NSObject (AutoEncodeDecode)
- (void)encodeWithCoder:(NSCoder *)encoder {
Class cls = [selfclass];
while (cls != [NSObjectclass]) {
unsigned int numberOfIvars =0;
Ivar* ivars = class_copyIvarList(cls, &numberOfIvars);
for(const Ivar* p = ivars; p < ivars+numberOfIvars; p++){
Ivar const ivar = *p;
const char *type =ivar_getTypeEncoding(ivar);
NSString *key = [NSStringstringWithUTF8String:ivar_getName(ivar)];
id value = [selfvalueForKey:key];
if (value) {
switch (type[0]) {
case _C_STRUCT_B: {
NSUInteger ivarSize =0;
NSUInteger ivarAlignment =0;
NSGetSizeAndAlignment(type, &ivarSize, &ivarAlignment);
NSData *data = [NSDatadataWithBytes:(constchar *)self + ivar_getOffset(ivar)
length:ivarSize];
[encoder encodeObject:dataforKey:key];
}
break;
default:
[encoder encodeObject:value
forKey:key];
break;
}
}
}
free(ivars);
cls = class_getSuperclass(cls);
}
}
- (id)initWithCoder:(NSCoder *)decoder {
self = [self init];
if (self) {
Class cls = [selfclass];
while (cls != [NSObjectclass]) {
unsigned int numberOfIvars =0;
Ivar* ivars = class_copyIvarList(cls, &numberOfIvars);
for(constIvar* p = ivars; p < ivars+numberOfIvars; p++){
Ivar const ivar = *p;
const char *type =ivar_getTypeEncoding(ivar);
NSString *key = [NSStringstringWithUTF8String:ivar_getName(ivar)];
id value = [decoder decodeObjectForKey:key];
if (value) {
switch (type[0]) {
case _C_STRUCT_B: {
NSUInteger ivarSize =0;
NSUInteger ivarAlignment =0;
NSGetSizeAndAlignment(type, &ivarSize, &ivarAlignment);
NSData *data = [decoderdecodeObjectForKey:key];
char *sourceIvarLocation = (char*)self+ivar_getOffset(ivar);
[data getBytes:sourceIvarLocationlength:ivarSize];
}
break;
default:
[self setValue:[decoder decodeObjectForKey:key]
forKey:key];
break;
}
}
}
free(ivars);
cls = class_getSuperclass(cls);
}
}
return self;
}