一、概述
協議定義了一個綱領性的介面,所有類都可以選擇實現。它主要是用來定義一套物件之間的通訊規則。protocol也是我們設計時常用的一個東西,相對於直接繼承的方式,protocol則偏向於組合模式。他使得兩個毫不相關的類能夠相互通訊,從而實現特定的目標。因為OC是單繼承的,由於不支援多繼承,所以很多時候都是用Protocol和Category來代替實現"多繼承"。
二、底層實現
在objc4-723中protocol的定義如下:
struct protocol_t : objc_object {
const char *mangledName;
struct protocol_list_t *protocols;
method_list_t *instanceMethods;
method_list_t *classMethods;
method_list_t *optionalInstanceMethods;
method_list_t *optionalClassMethods;
property_list_t *instanceProperties;
uint32_t size; // sizeof(protocol_t)
uint32_t flags;
// Fields below this point are not always present on disk.
const char **_extendedMethodTypes;
const char *_demangledName;
property_list_t *_classProperties;
const char *demangledName();
...
};
複製程式碼
我們可以看到protocol繼承自objc_object,裡面的欄位基本算是清晰,主要結構如下:
-
mangledName 和 _demangledName
這是來源於C++的name mangling(命名重整)技術,在C++裡面用來區別過載是的函式。 -
protocols
它是protocol_list_t型別的指標,儲存了這個協議所遵守的協議; -
instanceMethods
例項方法列表 -
calssMethods
類方法列表 -
optionalInstanceMethods
可選擇實現的例項方法,在宣告時用@optional關鍵字修飾的例項方法 -
optionalClassMethods
可選擇實現的類方法,在宣告時用@optional關鍵字修飾的類方法 -
instanceProperties
例項屬性 -
_classProperties
類屬性,比較少見@property (nonatomic, strong) NSString *name;//通過例項呼叫 @property (class, nonatomic, strong) NSString *className;//通過類名呼叫 複製程式碼
三、 常用方法
- protocol_copyPropertyList
runtime提供了兩個方法:
objc_property_t *
protocol_copyPropertyList(Protocol *proto, unsigned int *outCount)
{
return protocol_copyPropertyList2(proto, outCount,
YES/*required*/, YES/*instance*/);
}
objc_property_t *
protocol_copyPropertyList2(Protocol *proto, unsigned int *outCount,
BOOL isRequiredProperty, BOOL isInstanceProperty)
{
if (!proto || !isRequiredProperty) {
// Optional properties are not currently supported.
if (outCount) *outCount = 0;
return nil;
}
rwlock_reader_t lock(runtimeLock);
property_list_t *plist = isInstanceProperty
? newprotocol(proto)->instanceProperties
: newprotocol(proto)->classProperties();
return (objc_property_t *)copyPropertyList(plist, outCount);
}
複製程式碼
// Optional properties are not currently supported.
這裡指明瞭可選屬性現在還不支援,這就是沒有可選屬性的原因
- conformsToProtocol()
+ (BOOL)conformsToProtocol:(Protocol *)protocol {
if (!protocol) return NO;
for (Class tcls = self; tcls; tcls = tcls->superclass) {
if (class_conformsToProtocol(tcls, protocol)) return YES;
}
return NO;
}
- (BOOL)conformsToProtocol:(Protocol *)protocol {
if (!protocol) return NO;
for (Class tcls = [self class]; tcls; tcls = tcls->superclass) {
if (class_conformsToProtocol(tcls, protocol)) return YES;
}
return NO;
}
複製程式碼
兩個方法都是遍歷類的繼承集體,呼叫class_conformsToProtocol
方法,其實現如下:
BOOL class_conformsToProtocol(Class cls, Protocol *proto_gen)
{
protocol_t *proto = newprotocol(proto_gen);
if (!cls) return NO;
if (!proto_gen) return NO;
rwlock_reader_t lock(runtimeLock);
assert(cls->isRealized());
for (const auto& proto_ref : cls->data()->protocols) {
protocol_t *p = remapProtocol(proto_ref);
if (p == proto || protocol_conformsToProtocol_nolock(p, proto)) {
return YES;
}
}
return NO;
}
複製程式碼
把class的protocols取出來,並與傳入的protocol做比較,如果地址相同直接返回,或者協議"繼承"的層級中滿足條件:
/***********************************************************************
* protocol_conformsToProtocol_nolock
* Returns YES if self conforms to other.
* Locking: runtimeLock must be held by the caller.
**********************************************************************/
static bool
protocol_conformsToProtocol_nolock(protocol_t *self, protocol_t *other)
{
runtimeLock.assertLocked();
if (!self || !other) {
return NO;
}
// protocols need not be fixed up
if (0 == strcmp(self->mangledName, other->mangledName)) {
return YES;
}
if (self->protocols) {
uintptr_t i;
for (i = 0; i < self->protocols->count; i++) {
protocol_t *proto = remapProtocol(self->protocols->list[i]);
if (0 == strcmp(other->mangledName, proto->mangledName)) {
return YES;
}
if (protocol_conformsToProtocol_nolock(proto, other)) {
return YES;
}
}
}
return NO;
}
複製程式碼
遞迴處理,對比協議的mangledName,有相同的就返回YES。
參考:
協議protocol
Protocol