Clang官方文檔:Clang documentation
objc_runtime_name
用于 @interface 或 @protocol尚卫,指定類或協(xié)議編譯后的名字
attribute((objc_runtime_name("MyLocalName")))
@interface Message
@end
// (lldb) po [self class]
// MyLocalNameobjc_requires_super
指定子類覆寫該方法時需要調(diào)用父類方法
The attribute to designate that a method requires a “call to super
” in the overriding method in the subclass.
- (void)foo __attribute__((objc_requires_super));
-
objc_subclassing_restricted
禁止繼承
attribute((objc_subclassing_restricted))
@interface A : NSObject
@end@interface B : A // Cannot subclass a class with objc_subclassing_restricted attribute @end
objc_runtime_visible
禁止繼承和category
This attribute specifies that the Objective-C class to which it applies is visible to the Objective-C runtime but not to the linker. Classes annotated with this attribute cannot be subclassed and cannot have categories defined for them.
-
constructor / destructor
添加這兩個屬性的函數(shù)會在分別在可執(zhí)行文件(或 shared library)load 和 unload 時被調(diào)用她奥,即main()函數(shù)調(diào)用前和main()函數(shù)return后執(zhí)行,執(zhí)行順序:+load -> constructor -> main -> destructor__attribute__((constructor)) static void beforeMain(void) { NSLog(@"%s", __func__); // beforeMain } __attribute__((destructor)) static void afterMain(void) { NSLog(@"%s", __func__); // afterMain (main函數(shù)return后執(zhí)行) }
constructor可控制優(yōu)先級娜氏,如__attribute__((constructor(101))),數(shù)字越小優(yōu)先級越高烙丛,1-100為系統(tǒng)保留。
-
cleanup
聲明的變量作用域結(jié)束后調(diào)用指定函數(shù){ __strong XXObject *obj __attribute__((cleanup(cleanuptest))) = [[XXObject alloc]init]; [obj test]; } NSLog(@"end");
static void cleanuptest(__strong XXObject **obj) { XXObject *obj_t = *obj; NSLog(@"%s", __func__); } // 2016-05-25 15:38:08.350 Test[2559:159531] cleanuptest // 2016-05-25 15:38:08.350 Test[2559:159531] -[XXObject dealloc] // 2016-05-25 15:38:08.350 Test[2559:159531] end