生活随笔
收集整理的這篇文章主要介紹了
__attribute__函数的作用
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
[iOS]__attribute__
標簽:?iOS 2016-09-07 19:41?
107人閱讀??
收藏?
舉報
?
分類: iOS開發(fā)(52)?
版權聲明:本文為博主原創(chuàng)文章,未經博主允許不得轉載。
目錄(?)[+]
今天看博客看到了sunnyxx大神介紹了關于attribute的一個黑魔法,并且我在ibirme大神的源代碼里面也看到他用過這個屬性,我就系統(tǒng)的學習了一下,記錄一下常用的方法。
__attribute__((format())) ?
這個format有3個參數。
[objc]?view plaincopy
int?my(NSString?*str,NSString?*str1,NSArray*str2,...)?__attribute__((format(__NSString__,2,4)));?? 三個參數告訴編譯器,第二個參數必須是NSString類型,且可變參數從第4位開始。
如果你把第二個參數改成別的類型,或者加一個參數,使可變參數變成了第五個,這都是不行的。
__attribute__((nonnull())) ?
這個參數可以無數多個
[objc]?view plaincopy
int?my(NSString?*str,NSString?*str1,NSArray*str2,...)?__attribute__((nonnull(1,3)));?? 這個的意思是,第一個和第三個參數不能為空。
__attribute__((noreturn))
此方法沒有參數,表示這個函數沒有返回值也不能有返回值。
?__attribute__((const)) ?
這個表示一個方法的返回值只由參數決定,如果參數不變的話,就不再調用此函數,直接返回值。 經過我的嘗試發(fā)現還是調用了,后又經查資料發(fā)現要給gcc加一個-O的參數才可以。是對函數調用的一種優(yōu)化。
__attribute__((availability))
__attribute__((availability(ios,introduced=2_0,deprecated=7_0)));
表示此函數應用于ios平臺,從2.0版本到7.0版本. 這種調用在foundation頭文件中經常見到,一些比較著名,存在時間比較長的開源庫也有。
__attribute__((unused))
表示函數的返回值必須被檢查或使用,否則會警告。
__attribute__((cleanup()))
可以定義一個變量,在他的作用域結束的時候會自動執(zhí)行一個指定的方法,該方法執(zhí)行在dealloc之前。
#define onExit\
__strong void(^block)(void) __attribute__((cleanup(blockCleanUp), unused)) = ^ 一個巧妙的用法就是像上面一樣定義一個宏,然后
{
onExit {
NSLog(@"yo");
};
} 在這個onExit中的代碼就會在最后執(zhí)行,這段是在sunnyxx的博客中看到的,應用于reactive cocoa。
__attribute__((always_inline))
這段代碼能夠保證代碼是內聯的,因為你如果只定義內聯的話,編譯器并不一定會以內聯的方式調用,如果代碼太多你就算用了內聯也不一定會內聯,用了這個的話會強制內聯。
[objc]?view plaincopy
static?__inline__?__attribute__((always_inline))?? 將這段代碼定義成一個宏,然在函數的前邊就能直接強制內聯,如果是頻繁調用的函數,這樣可以提高一定的效率。
在系統(tǒng)的base.h文件中,蘋果為很多屬性定義了宏,有下面這些
[objc]?view plaincopy
#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))?? 都是可以直接使用的。
總結
以上是生活随笔為你收集整理的__attribute__函数的作用的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。