__attribute__((format()))
這個format有3個參數。
int my(NSString *str,NSString *str1,NSArray*str2,...) __attribute__((format(__NSString__,2,4)));
三個參數告訴編譯器,第二個參數必須是NSString類型,且可變參數從第4位開始。
如果你把第二個參數改成別的類型,或者加一個參數,使可變參數變成了第五個,這都是不行的。
__attribute__((nonnull()))
這個參數可以無數多個
int my(NSString *str,NSString *str1,NSArray*str2,...) __attribute__((nonnull(1,3)));
這個的意思是,第一個和第三個參數不能為空。
__attribute__((noreturn))
此方法沒有參數,表示這個函數沒有返回值也不能有返回值。
__attribute__((const))
這個表示一個方法的返回值只由參數決定,如果參數不變的話,就不再調用此函數,直接返回值。
經過我的嘗試發現還是調用了,后又經查資料發現要給gcc加一個-O的參數才可以。是對函數調用的一種優化。
__attribute__((availability))
__attribute__((availability(ios,introduced=2_0,deprecated=7_0)));
表示此函數應用於ios平台,從2.0版本到7.0版本.
這種調用在foundation頭文件中經常見到,一些比較著名,存在時間比較長的開源庫也有。
__attribute__((unused))
表示函數的返回值必須被檢查或使用,否則會警告。
__attribute__((used))
表示函數可能不會調用、可能用不到,編譯器不要提醒
__attribute__((cold))
表示函數不經常調用
__attribute__((cleanup()))
可以定義一個變量,在他的作用域結束的時候會自動執行一個指定的方法,該方法執行在dealloc之前。
#define onExit\
__strong void(^block)(void) __attribute__((cleanup(blockCleanUp), unused)) = ^
一個巧妙的用法就是像上面一樣定義一個宏,然后
{
onExit {
NSLog(@"yo");
};
}
在這個onExit中的代碼就會在最后執行,這段是在sunnyxx的博客中看到的,應用於reactive cocoa。
__attribute__((always_inline))
這段代碼能夠保證代碼是內聯的,因為你如果只定義內聯的話,編譯器並不一定會以內聯的方式調用,如果代碼太多你就算用了內聯也不一定會內聯,用了這個的話會強制內聯。
static __inline__ __attribute__((always_inline))
將這段代碼定義成一個宏,然在函數的前邊就能直接強制內聯,如果是頻繁調用的函數,這樣可以提高一定的效率。
在系統的base.h文件中,蘋果為很多屬性定義了宏,有下面這些
#define OS_NORETURN __attribute__((__noreturn__))
#define OS_NOTHROW __attribute__((__nothrow__))
#define OS_NONNULL1 __attribute__((__nonnull__(1)))
#define OS_NONNULL2 __attribute__((__nonnull__(2)))
#define OS_NONNULL3 __attribute__((__nonnull__(3)))
#define OS_NONNULL4 __attribute__((__nonnull__(4)))
#define OS_NONNULL5 __attribute__((__nonnull__(5)))
#define OS_NONNULL6 __attribute__((__nonnull__(6)))
#define OS_NONNULL7 __attribute__((__nonnull__(7)))
#define OS_NONNULL8 __attribute__((__nonnull__(8)))
#define OS_NONNULL9 __attribute__((__nonnull__(9)))
#define OS_NONNULL10 __attribute__((__nonnull__(10)))
#define OS_NONNULL11 __attribute__((__nonnull__(11)))
#define OS_NONNULL12 __attribute__((__nonnull__(12)))
#define OS_NONNULL13 __attribute__((__nonnull__(13)))
#define OS_NONNULL14 __attribute__((__nonnull__(14)))
#define OS_NONNULL15 __attribute__((__nonnull__(15)))
#define OS_NONNULL_ALL __attribute__((__nonnull__))
#define OS_SENTINEL __attribute__((__sentinel__))
#define OS_PURE __attribute__((__pure__))
#define OS_CONST __attribute__((__const__))
#define OS_WARN_RESULT __attribute__((__warn_unused_result__))
#define OS_MALLOC __attribute__((__malloc__))
#define OS_USED __attribute__((__used__))
#define OS_UNUSED __attribute__((__unused__))
#define OS_WEAK __attribute__((__weak__))
#define OS_WEAK_IMPORT __attribute__((__weak_import__))
#define OS_NOINLINE __attribute__((__noinline__))
#define OS_ALWAYS_INLINE __attribute__((__always_inline__))
#define OS_TRANSPARENT_UNION __attribute__((__transparent_union__))
#define OS_ALIGNED(n) __attribute__((__aligned__((n))))
#define OS_FORMAT_PRINTF(x,y) __attribute__((__format__(printf,x,y)))
#define OS_EXPORT extern __attribute__((__visibility__("default")))
#define OS_INLINE static __inline__
#define OS_EXPECT(x, v) __builtin_expect((x), (v))
都是可以直接使用的。