在了解app啟動(dòng)優(yōu)化的時(shí)候蒿往,總是看到有一個(gè)過程是執(zhí)行聲明為
__attribute__((constructor))
的C函數(shù)嘀倒,所以就想了解下這個(gè)東西是干什么的剩檀。查相關(guān)資料發(fā)現(xiàn)這東西很強(qiáng)大限佩。
attribute 介紹
attribute是一個(gè)編譯屬性熊尉,用于向編譯器描述特殊的標(biāo)識(shí)、錯(cuò)誤檢查或高級(jí)優(yōu)化锻狗。它是GNU C特色之一满力,系統(tǒng)中有許多地方使用到。 attribute可以設(shè)置函數(shù)屬性(Function Attribute )轻纪、變量屬性(Variable Attribute )和類型屬性(Type Attribute)等油额。
attribute 格式
__attribute__ ((attribute-list))
其位置約束為:放于聲明的尾部“;”之前桐磁。
函數(shù)屬性
函數(shù)屬性可以幫助開發(fā)者把一些特性添加到函數(shù)聲明中悔耘,從而可以使編譯器在錯(cuò)誤檢查方面的功能更強(qiáng)大讲岁。
- noreturn
- noinline
- always_inline
- pure
- const
- nothrow
- sentinel
- format
- format_arg
- no_instrument_function
- section
- constructor
- destructor
- used
- unused
- deprecated
- weak
- malloc
- alias
- warn_unused_result
- nonnull
類型屬性
- aligned
- packed
- transparent_union,
- unused,
- deprecated
- may_alias
變量屬性
- aligned
- packed
Clang特有的
- availability
- overloadable
常見屬性
1. format
語法為attribute((format(NSString, F, A)))我擂,可以給被聲明的函數(shù)加上類似printf或者scanf的特征,它可以使編譯器檢查函數(shù)聲明和函數(shù)實(shí)際調(diào)用參數(shù)之間的格式化字符串是否匹配缓艳。format (archetype, m, n)校摩,第一個(gè)參數(shù)傳遞archetype指定為哪種類型,string-index指定格式化字符串的位置阶淘,n指定可變參數(shù)檢查開始的位置衙吩。
format (archetype, string-index, first-to-check)
在Objective-C 中通過使用NSString格式達(dá)到同樣的效果,就像在NSString +stringWithFormat:
和NSLog()
里使用字符串格式一樣
FOUNDATION_EXPORT void NSLog(NSString *format, ...) NS_FORMAT_FUNCTION(1,2);
+ (instancetype)stringWithFormat:(NSString *)format, ... NS_FORMAT_FUNCTION(1,2);
在使用NSLog函數(shù)進(jìn)行輸出時(shí)溪窒,如果我們傳入的可變參數(shù)沒有在格式化字符串中使用坤塞,編譯器會(huì)提示警告,如下:
2.constructor與destructor
constructor屬性可以指定函數(shù)在main函數(shù)執(zhí)行之前進(jìn)行調(diào)用澈蚌,與之對(duì)應(yīng)destructor可以指定某個(gè)函數(shù)在main函數(shù)執(zhí)行結(jié)束之后再執(zhí)行摹芙。這是一種非常強(qiáng)大的機(jī)制,在實(shí)際應(yīng)用中也非常頻繁宛瞄,例如對(duì)以一個(gè)擁有模塊化和路由功能的應(yīng)用程序浮禾,可以通過這種方式來自動(dòng)化的進(jìn)行路由注冊(cè)(無需手動(dòng)調(diào)用),需要注意份汗,constructor與destructor屬性都可以設(shè)置一個(gè)優(yōu)先級(jí)參數(shù)盈电,優(yōu)先級(jí)高的函數(shù)會(huì)先執(zhí)行(0-100的優(yōu)先級(jí)為系統(tǒng)保留)
void __attribute__((constructor(101))) func1() {
NSLog(@"Func1");
}
void __attribute__((constructor(102))) func2() {
NSLog(@"Func2");
}
void __attribute__((destructor(101))) func3() {
NSLog(@"Func3");
}
void __attribute__((destructor(102))) func4() {
NSLog(@"Func4");
}
// 會(huì)依次打印
/*
2020-04-27 16:05:57.270522+0800 TestDemo[33430:13458931] Func1
2020-04-27 16:05:57.271017+0800 TestDemo[33430:13458931] Func2
2020-04-27 16:05:57.271193+0800 TestDemo[33430:13458931] main函數(shù)執(zhí)行
2020-04-27 16:05:57.271273+0800 TestDemo[33430:13458931] Func4
2020-04-27 16:05:57.271316+0800 TestDemo[33430:13458931] Func3
*/
int main(int argc, const char * argv[]) {
@autoreleasepool {
NSLog(@"main函數(shù)執(zhí)行");
}
return 0;
}
3. unavailable
告訴編譯器該方法不可用,如果強(qiáng)行調(diào)用編譯器會(huì)提示錯(cuò)誤杯活。比如某個(gè)類在構(gòu)造的時(shí)候不想直接通過init來初始化匆帚,只能通過特定的初始化方法()比如單例,就可以將init方法標(biāo)記為unavailable;
//系統(tǒng)的宏,可以直接拿來用
#define UNAVAILABLE_ATTRIBUTE __attribute__((unavailable))
#define NS_UNAVAILABLE UNAVAILABLE_ATTRIBUTE
@interface Person : NSObject
@property(nonatomic,copy) NSString *name;
@property(nonatomic,assign) NSUInteger age;
- (instancetype)init NS_UNAVAILABLE;
- (instancetype)initWithName:(NSString *)name age:(NSUInteger)age;
@end
實(shí)際上unavailable后面可以跟參數(shù),顯示一些信息,如:
//系統(tǒng)的
#define NS_AUTOMATED_REFCOUNT_UNAVAILABLE __attribute__((unavailable("not available in automatic reference counting mode")))
4.objc_root_class
表示這個(gè)類是一個(gè)根類(基類),比如NSObject,NSProxy.
//摘自系統(tǒng)
//NSProxy
NS_ROOT_CLASS
@interface NSProxy <NSObject> {
Class isa;
}
//NSObject
__OSX_AVAILABLE_STARTING(__MAC_10_0, __IPHONE_2_0)
OBJC_ROOT_CLASS
OBJC_EXPORT
@interface NSObject <NSObject> {
Class isa OBJC_ISA_AVAILABILITY;
}
5.overloadable
用于c語言函數(shù),可以定義若干個(gè)函數(shù)名相同旁钧,但參數(shù)不同的方法卷扮,調(diào)用時(shí)編譯器會(huì)自動(dòng)根據(jù)參數(shù)選擇函數(shù)原型:
__attribute__((overloadable)) void print(NSString *string){
NSLog(@"%@",string);
}
__attribute__((overloadable)) void print(int num){
NSLog(@"%d",num);
}
//調(diào)用
print(10);
print(@"哈哈");
6. objc_subclassing_restricted
指明當(dāng)前類型不能有子類荡澎,相當(dāng)于final關(guān)鍵字,語法為attribute((objc_subclassing_restricted))晤锹。例如:
__attribute__((objc_subclassing_restricted))
@interface FinalObject : NSObject
7. objc_requires_super
表示子類重寫當(dāng)前類的方法時(shí)摩幔,必須要調(diào)用super函數(shù),否則會(huì)有警告鞭铆。語法為__attribute__((objc_requires_super))
或衡,例如:
- (void)fatherMethod __attribute__((objc_requires_super));
8.objc_designated_initializer
指定內(nèi)部實(shí)現(xiàn)的初始化方法,系統(tǒng)宏NS_DESIGNATED_INITIALIZER展開即為該指令车遂,語法為__attribute__((objc_designated_initializer))
封断。例如:
- (instancetype)initNoDesignated ;
- (instancetype)initNoDesignated1 NS_DESIGNATED_INITIALIZER;
當(dāng)一個(gè)類存在方法帶有NS_DESIGNATED_INITIALIZER屬性時(shí),它的NS_DESIGNATED_INITIALIZER方法必須調(diào)用super的NS_DESIGNATED_INITIALIZER方法舶担。它的其他方法(非NS_DESIGNATED_INITIALIZER)只能調(diào)用self的方法初始化坡疼。
參考地址
http://fighting300.com/2016/06/12/iOS-attribute/
http://www.reibang.com/p/965f6f903114
http://www.reibang.com/p/29eb7b5c8b2d
https://cloud.tencent.com/developer/article/1622209