關於__has_attribute的使用

在開源代碼中咱們常常看到以下的代碼html

#ifndef ASDISPLAYNODE_REQUIRES_SUPER
#if __has_attribute(objc_requires_super)
#define ASDISPLAYNODE_REQUIRES_SUPER __attribute__((objc_requires_super))
#else
#define ASDISPLAYNODE_REQUIRES_SUPER
#endif
#endif


一直很疑惑c#

__attribute__((objc_requires_super))ide

是幹嗎用的,今天特意查了下,官網解釋以下:ui

Some Objective-C classes allow a subclass to override a particular method in a parent class but expect that the overriding method also calls the overridden method in the parent class. For these cases, we provide an attribute to designate that a method requires a 「call to super」 in the overriding method in the subclass.spa

簡單來講就是子類繼承父類的某個方法時,若是在父類的該方法後面加了該屬性,子類中若是沒有調用父類的super方法,編譯器則會有告警。code

以下圖:htm

更多的相關介紹見Clang官網介紹繼承

相關文章
相關標籤/搜索