1.添加方法:
class_addMethod(<#__unsafe_unretained Class cls#>, <#SEL name#>, <#IMP imp#>, <#const char *types#>)
#import <Foundation/Foundation.h>
#include <objc/runtime.h>
@interface Fruit : NSObject
@property (nonatomic , copy) NSString *name ;
@property (nonatomic , assign) CGFloat price ;
@end
@implementation Fruit
@end
int main(int argc, const char * argv[]) {
Fruit *fruit = [[Fruit alloc] init] ;
fruit.price = 1.5 ;
fruit.name = @"蘋果" ;
//IMP imp:函數體:
IMP imp = imp_implementationWithBlock(^NSString *(Fruit *self) {
NSMutableString *mString = [NSMutableString string] ;
unsigned int count = 0 ;
//獲取屬性列表:
objc_property_t *properties = class_copyPropertyList([Fruit class], &count) ;
for (unsigned int i = 0; i < count; ++i) {
//遍歷屬性列表獲取屬性列表里的屬性:
objc_property_t property = properties[i] ;
//獲取完屬性列表里的屬性的同時也獲取他們的屬性名:這些屬性名的返回值類型是C語言字符串:
const char *cName = property_getName(property) ;
//C語言轉OC語言
NSString *ocName = [NSString stringWithUTF8String:cName] ;
//把這些屬性追加到開始創建的空的可變字符串里:
[mString appendFormat:@"%@ --> %@" , ocName , [self valueForKey:ocName]] ;
}
free(properties) ;
//返回最終的自定義description方法:
return [NSString stringWithFormat:@"custom description:%@" , mString] ;
}) ;
//給類添加方法:
class_addMethod(NSClassFromString(@"Fruit"), @selector(description), imp, "@@:") ;
//const char *types:types是一個"字符數組" ,第一個字符是返回值類型 , 第二個字符是self的類型 , 第三個是_cmd方法的類型 , 返回值類型void用 v 表示 , 如果返回值類型是對象類型id 則用 @ 表示 , 注意OC當中的任何對象類型都可以用id表示 , 這里的NSString也應該寫為 @ 即可! ; self也是對象類型 , 也用 @ 表示 ; _cmd用 : 表示 ,注意_cmd是SEL類型,就用 : 表示 ;
//所以按照返回值類型 + self類型 + _cmd類型的格式 , 這里應該寫為:"@@:" 即可!
NSLog(@"&&&&&&%@&&&&&&&" , [fruit description]) ;
return 0;
}
給NSObject(替換description方法)
替換方法:如果本類已經有了這個方法我不能再給它添加了,只能重寫該方法 , 在
runtime
里我們用替換方法來表示:
class_replaceMethod(<#__unsafe_unretained Class cls#>, <#SEL name#>, <#IMP imp#>, <#const char *types#>)
通過Method
獲取代碼實現IMP方法
用的是method_getImplementation
這個方法 , 通過Method
獲取type Encoding
類型編碼用的是method_getTypeEncoding
這個方法!
重要:
SEL:消息名稱 ; IMP:實現代碼 ; Method: SEL + IMP + 其他(包含type Encoding , 參數...)
#import <Foundation/Foundation.h>
#include <objc/runtime.h>
@interface Fruit : NSObject
@property (nonatomic , copy) NSString *name ;
@property (nonatomic , assign) CGFloat price ;
@end
@implementation Fruit
@end
//構建NSObject的分類:
@interface NSObject (CustomDescription)
@end
@implementation NSObject (CustomDescription)
//最先調用的方法:加載進內存的方法
+ (void)load {
//SEL:消息名稱 ; IMP:實現代碼 ; Method: SEL + IMP + 其他(包含type Encoding , 參數...)
//取出實現:
Method m1 = class_getInstanceMethod([NSObject class], @selector(myDescription)) ;
//從方法中獲取實現代碼:
// IMP imp = method_getImplementation(m1) ;
//從方法中獲取類型編碼:
const char *type = method_getTypeEncoding(m1) ;
NSLog(@"type = %@" , @(type)) ;
//下面兩種方式:一種是直接替換 , 一種是交換方法 , 如果用第一種那么系統自己的description方法就找不到了就被完全替換了,再也沒有description方法了,而第二種是保留系統原有的description方法只不過創建一個新的myDescription方法用來和原有的description方法進行交換:
//給類替換方法:
// class_replaceMethod([NSObject class], @selector(description),imp , type) ;
Method m2 = class_getInstanceMethod([NSObject class], @selector(description)) ;
//交換方法:
method_exchangeImplementations(m1, m2) ;
//這就是Method Swizzle , 俗稱Runtime黑魔法 ;
}
- (NSString *)myDescription {
NSMutableString *mString = [NSMutableString string] ;
unsigned int count = 0 ;
objc_property_t *properties = class_copyPropertyList([Fruit class], &count) ;
for (unsigned int i = 0; i < count; ++i) {
objc_property_t property = properties[i] ;
const char *cName = property_getName(property) ;
NSString *ocName = [NSString stringWithUTF8String:cName] ;
[mString appendFormat:@"%@ --> %@" , ocName , [self valueForKey:ocName]] ;
}
free(properties) ;
//檢測當前的代碼實現:
NSLog(@"current method selector : %@" , NSStringFromSelector(_cmd)) ;
return [NSString stringWithFormat:@"%@-------%@" , [self myDescription], mString] ;
}
@end
int main(int argc, const char * argv[]) {
Fruit *fruit = [[Fruit alloc] init] ;
fruit.price = 1.5 ;
fruit.name = @"蘋果" ;
NSLog(@"myDescription = %@ " , [fruit description]) ;
return 0;
}