11、系统类的方法实现部分替换
- (void) methodExchange {
Method m1 = class_getInstanceMethod([NSStringclass],@selector(lowercaseString));
Method m2 = class_getInstanceMethod([NSStringclass],@selector(uppercaseString));
method_exchangeImplementations(m1, m2);
NSLog(@"%@", [@"sssAAAAss"lowercaseString]);
NSLog(@"%@", [@"sssAAAAss"uppercaseString]);
}
打印结果:(仔细看log)
2013-07-26 16:33:22.776 HighOC[7104:c07] SSSAAAASS
2013-07-26 16:33:22.778 HighOC[7104:c07] sssaaaass
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;
}
IOS高级开发~Runtime(一)
http://blog.csdn.net/zfpp25_/article/details/9496705
IOS高级开发~Runtime(二)
http://blog.csdn.net/zfpp25_/article/details/9497187
IOS高级开发~Runtime(三)
http://blog.csdn.net/zfpp25_/article/details/9497721
IOS高级开发~Runtime(四)
http://blog.csdn.net/zfpp25_/article/details/9498233