一、寫在前面
Objective-C語言是動(dòng)態(tài)語言,在編譯階段并不決定調(diào)用那個(gè)函數(shù)物咳,只有在運(yùn)行的時(shí)候才去根據(jù)函數(shù)名調(diào)用對(duì)應(yīng)的函數(shù),稱為OC的動(dòng)態(tài)特性蹄皱。Objective-C一系列動(dòng)態(tài)特性的實(shí)現(xiàn)用的就是runtime览闰。
Objective-C方法調(diào)用實(shí)質(zhì)就是消息機(jī)制的轉(zhuǎn)發(fā),即讓對(duì)象發(fā)送消息夯接。
如:
People *p = [[People alloc] init];
[p eat];
調(diào)用eat方法的實(shí)質(zhì)就是:
objc_msgSend(p, @selector(eat));
其消息機(jī)制的轉(zhuǎn)發(fā)原理就是對(duì)象通過SEL去映射表中查找對(duì)應(yīng)的方法實(shí)現(xiàn)焕济。
二、動(dòng)態(tài)處理方法
1.替換方法
方法的替換指的是將原有的方法實(shí)現(xiàn)更改為自己制定的方法實(shí)現(xiàn)盔几,通過原有的方法名調(diào)用方法時(shí)執(zhí)行自己指定的方法晴弃。
我們更換下面兩個(gè)類的方法做一下替換:
@implementation People
- (void)eat
{
NSLog(@"%s", __FUNCTION__);
}
@end
@implementation Dog
- (void)sleep
{
NSLog(@"%s", __FUNCTION__);
}
@end
調(diào)用替換方法,將People的eat方法替換為Dog的sleep方法:
ReplaceMethod([People class], @selector(eat), [Dog class], @selector(sleep));
ReplaceMethod方法的實(shí)現(xiàn)如下:
void ReplaceMethod(Class originClass, SEL originSelector, Class targetClass, SEL targetSelector)
{
Method targetMethod = class_getInstanceMethod(targetClass, targetSelector);
class_replaceMethod(originClass, originSelector, method_getImplementation(targetMethod), method_getTypeEncoding(targetMethod));
}
調(diào)用People的eat方法看到的輸出為Dog的sleep方法的打友放摹:
People *p = [[People alloc] init];
[p eat];
2.交換方法
方法交換是指交換兩個(gè)方法的實(shí)現(xiàn)上鞠,如有甲、乙兩個(gè)方法芯丧,調(diào)用甲方法實(shí)際上調(diào)用的是乙方法的實(shí)現(xiàn)芍阎,調(diào)用乙方法實(shí)際上調(diào)用的是甲方法的實(shí)現(xiàn)。
還是People類和Dog類缨恒,交換兩個(gè)類的方法:
ExchangeInstanceMethod([People class], @selector(eat), [Dog class], @selector(sleep));
ExchangeInstanceMethod方法的實(shí)現(xiàn):
void ExchangeInstanceMethod(Class firstClass, SEL firstSelector, Class secondClass, SEL secondSelector)
{
Method firstMethod = class_getInstanceMethod(firstClass, firstSelector);
Method secondMethod = class_getInstanceMethod(secondClass, secondSelector);
method_exchangeImplementations(firstMethod, secondMethod);
}
調(diào)用People的eat方法:
People *p = [[People alloc] init];
[p eat];
打印效果可以看到調(diào)用的是Dog的sleep方法:
調(diào)用Dog的sleep方法:
Dog *d = [[Dog alloc] init];
[d sleep];
打印效果可以看出調(diào)用的是People的eat方法:
3.添加方法:
類可以通過performSelector直接調(diào)用類的方法谴咸,如:
People *p = [[People alloc] init];
[p eat];
可以這樣調(diào)用:
People *p = [[People alloc] init];
[p performSelector:@selector(eat)];
class_addMethod為類添加一個(gè)未曾實(shí)現(xiàn)的方法,可以通過performSelector進(jìn)行調(diào)用骗露。
如將Dog的sleep方法添加到People類上:
AddMethod([People class], @selector(sleep), [Dog class], @selector(sleep));
AddMethod方法的具體實(shí)現(xiàn):
void AddMethod(Class cls, SEL mSelector, Class lCls, SEL lSelector)
{
Method lMethod = class_getInstanceMethod(lCls, lSelector);
class_addMethod(cls, mSelector, method_getImplementation(lMethod), method_getTypeEncoding(lMethod));
}
通過performSelector調(diào)用People的sleep的方法岭佳,打印結(jié)果為Dog的sleep方法:
People *p = [[People alloc] init];
[p performSelector:@selector(sleep)];
三、獲取類的屬性和成員變量
1.獲取類的屬性
定義一個(gè)唱片的類MusicAlbum:
@interface MusicAlbum : NSObject
@property (nonatomic, strong) NSString *albumName;
@property (nonatomic, strong) NSString *author;
@property (nonatomic, strong) NSArray *musicList;
@property (nonatomic, assign) int64_t publishTime;
@end
通過class_copyPropertyList獲取類的屬性列表萧锉,再通過property_getName和property_getAttributes獲取屬性的名字和類型信息珊随。
unsigned int count = 0;
objc_property_t *propertys = class_copyPropertyList([MusicAlbum class], &count);
for (int i = 0; i < count; i++) {
objc_property_t property = propertys[i];
NSString *propertyName = [NSString stringWithUTF8String:property_getName(property)];
NSString *attriableName = [NSString stringWithUTF8String:property_getAttributes(property)];
NSLog(@"property name is: %@, -----> attriable is: %@", propertyName, attriableName);
}
輸出的屬性名對(duì)應(yīng)相應(yīng)的屬性類型,其中q代表的是長(zhǎng)整型。
Objective-C中基本數(shù)據(jù)類型包括短整型(short)叶洞、整形(int)鲫凶、長(zhǎng)整型(long)、浮點(diǎn)型(float)衩辟、雙精度浮點(diǎn)型(double)螟炫、字符型(char)、布爾型(BOOL)惭婿,對(duì)應(yīng)的標(biāo)識(shí)分別為:short(s)不恭、int(i)、long(q)财饥、float(f)换吧、double(d)、char(c)钥星、BOOL(B)沾瓦;引用數(shù)據(jù)類型以對(duì)應(yīng)的屬性類型顯示出來,如:musicList是數(shù)組類型谦炒,而數(shù)組又是引用類型贯莺,因此在屬性類型信息上可以看到T@"NSArray",&,N,V_musicList的信息。
2.獲取類的成員變量
對(duì)MusicAlbum類添加一些變量:
@private
int _age;
double _height;
@public
NSString *_nextMusicName;
通過class_copyIvarList獲取類的成員變量列表宁改,通過ivar_getName和ivar_getTypeEncoding獲取成員變量名和成員變量類型信息缕探。
unsigned int count = 0;
Ivar *ivars = class_copyIvarList([MusicAlbum class], &count);
for (int i = 0; i < count; i++) {
Ivar ivar = ivars[i];
NSString *ivarName = [NSString stringWithUTF8String:ivar_getName(ivar)];
NSString *ivarType = [NSString stringWithUTF8String:ivar_getTypeEncoding(ivar)];
NSLog(@"ivar name is: %@,-----> type is: %@", ivarName, ivarType);
}
在打印信息中可以看到通過Ivar獲取類的成員變量包括類的屬性、private變量和public變量还蹲。
成員變量類型信息和屬性類型信息是相同的爹耗,分為基本數(shù)據(jù)類型和引用數(shù)據(jù)類型,基本數(shù)據(jù)類型有相對(duì)應(yīng)的標(biāo)識(shí)谜喊,引用數(shù)據(jù)類型以其對(duì)應(yīng)的類型名標(biāo)識(shí)出來潭兽。
四、關(guān)聯(lián)對(duì)象
可以通過objc_setAssociatedObject和objc_getAssociatedObject給類添加屬性斗遏。
如通過分類給UIColor添加一個(gè)金色顏色屬性:
@interface UIColor (color)
@property (class, nonatomic, readonly) UIColor *goldColor;
@end
由于金色屬性是類屬性山卦,并且是一個(gè)只讀屬性,通過getter方法添加這個(gè)屬性诵次。
static NSString const *gold_color_key = @"gold_color_key";
@implementation UIColor (color)
+ (UIColor *)goldColor {
UIColor *color = objc_getAssociatedObject(self, &gold_color_key);
if (!color) {
color = [UIColor colorWithRed:1 green:1 blue:0 alpha:1.0];
objc_setAssociatedObject(self, &gold_color_key, color, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
return color;
}
@end
Demo
參考文章:
消息處理之performSelector
OC關(guān)聯(lián)對(duì)象小結(jié)(一)
剖析runtime,看完包懂
Objective-C 中的消息與消息轉(zhuǎn)發(fā)