語法
一般以attribute后面加參數(shù)
__attribute__(xx)
整理一下常用的废恋,如下。
1、deprecated
在編譯時會報過時警告
__attribute__((deprecated("使用#method2")));- (void)method12 DEPRECATED_ATTRIBUTE;//DEPRECATED_ATTRIBUTE是系統(tǒng)的宏
2、unavailable
NS_UNAVAILABLE、UNAVAILABLE_ATTRIBUTE、__attribute__((unavailable("不能用,不能用识埋,不能用")));
3、cleanup
- (void)cleanupDemo?{
????NSObject *myVar __attribute__((cleanup(cleanUpCallback))) = [[NSObject alloc] init];
????NSLog(@"%@",xcode);
}
// 指定一個cleanup方法扫俺,注意入?yún)⑹撬揎椬兞康牡刂酚颍愋鸵粯?/p>
// 對于指向objc對象的指針(id *),如果不強制聲明__strong默認是__autoreleasing,造成類型不匹配
static void cleanUpCallback(__strong NSObject **xcode){
NSLog(@"cleanUp call %@",*xcode);
}
4降允、availability 破停、unavailable
__attribute__((availability(ios,introduced=2_0,deprecated=3_0,message=""__VA_ARGS__)))
//系統(tǒng)的宏,可以直接拿來用#defineUNAVAILABLE_ATTRIBUTE __attribute__((unavailable))#defineNS_UNAVAILABLE UNAVAILABLE_ATTRIBUTE
5黑界、overloadable
這個屬性用在C的函數(shù)上實現(xiàn)像java一樣方法重載。直接上主菜:
__attribute__((overloadable))
void add(int num) {
????NSLog(@"Add Int %i",num);
}
__attribute__((overloadable))
void add(NSString* num) {
????NSLog(@"Add NSString %@",num);
}
__attribute__((overloadable))
void add(NSNumber* num) {
????NSLog(@"Add NSNumber %@",num);
}
6弦赖、?objc_designated_initializer
NS_DESIGNATED_INITIALIZER流酬、__attribute__((objc_designated_initializer))
7旦装、objc_subclassing_restricted
這個顧名思義就是相當于java的final關(guān)鍵字了阴绢,意是說它不能有子類腺兴。用于類
__attribute__((objc_subclassing_restricted))//Final類 ,java的final關(guān)鍵字@interfaceCustomObject:NSObject
8栈拖、objc_requires_super
這個也挺有意思的锻弓,意思是子類重寫這個方法的時候筋粗,必須調(diào)用[super xxx]
#define NS_REQUIRES_SUPER __attribute__((objc_requires_super))
- (void)method __attribute__((objc_requires_super));
9策橘、nonnull?
__attribute__((nonnull(1,2)))?
10、objc_boxable
實現(xiàn)類似于NSNumber 的快速打包能力@(...),一般對于struct,union我們只能通過NSValue將其打包. objc_boxable 可以幫助我們實現(xiàn)快速打包,示例如下:
//自定義結(jié)構(gòu)體
typedef struct __attribute__((objc_boxable)) { ?
CGFloatx,y,width,height;
}SGRect;
SGRect rect = {0,0,100,200};
//這里直接打包成
NSValue NSValue *value = @(rect);
//這里我直接用系統(tǒng)的方法打印NSLog(@"%@",NSStringFromCGRect(value.CGRectValue));
輸出:2016-07-2121:28:43.538Study[14118:5408921] {{0,0}, {100,200}}
11娜亿、constructor / destructor
意思是: 構(gòu)造器和析構(gòu)器;constructor修飾的函數(shù)會在main函數(shù)之前執(zhí)行,destructor修飾的函數(shù)會在程序exit前調(diào)用.?
__attribute__((constructor))voidbefore(){NSLog(@"before main");}
__attribute__((destructor))voidafter(){NSLog(@"after main");}
12丽已、enable_if?
用來檢查參數(shù)是否合法,只能用來修飾函數(shù):?
void printAge(int age) __attribute__((enable_if(age >0&& age <120,"?0 ~ 120"))) {
NSLog(@"%d",age);
}?
表示只能輸入的參數(shù)只能是 0 ~ 120左右,否則編譯報錯
13、objc_runtime_name?
看到runtime是不是就感覺高大上,沒錯這個也跟運行時有關(guān).作用是將將類或協(xié)議的名字在編譯時指定成另一個.示例如下:?
__attribute__((objc_runtime_name("NSObject")))
@interface SGObject:NSObject
@end
//調(diào)用NSLog(@"%@",[SGObject class]);
//輸出2016-07-2211:18:00.934 ?Study[14355:5516261] NSObject
14买决、objc_root_class
表示這個類是一個根類(基類),比如NSObject,NSProxy.?
//摘自系統(tǒng)
//NSProxy
NS_ROOT_CLASS
@interface NSProxy{
Class isa;
}
//
NSObject
__OSX_AVAILABLE_STARTING(__MAC_10_0,__IPHONE_2_0)
OBJC_ROOT_CLASS
OBJC_EXPORT
@interface NSObject?{
Class isa OBJC_ISA_AVAILABILITY;
}
15沛婴、warn_unused_result
當函數(shù)或者方法的返回值很重要時,要求調(diào)用者必須檢查或者使用返回值,否則編譯器會發(fā)出警告提示?
- (BOOL)availiable __attribute__((warn_unused_result)) {
????return 10;
}
文章引用:iOS中常用的Attribute
http://nshipster.com/__attribute__/
http://releases.llvm.org/3.8.0/tools/clang/docs/AttributeReference.html