__attribute__函数的作用
[iOS]__attribute__
版权声明:本文为博主原创文章,未经博主允许不得转载。
目录(?)[+]
今天看博客看到了sunnyxx大神介绍了关于attribute的一个黑魔法,并且我在ibirme大神的源代码里面也看到他用过这个属性,我就系统的学习了一下,记录一下常用的方法。
__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))
__attribute__((availability))
__attribute__((unused))
__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))
都是可以直接使用的。
__attribute__函数的作用相关推荐
- VC++ inline内联函数的作用解决方案
VC++ inline内联函数的作用解决方案 __attribute__((always_inline)) attribute((always_inline))的意思是强制内联,所有加了attribu ...
- c语言中inline函数的作用
记录一下inline函数的作用 普通函数的调用过程 在eclipse里面创建工程,代码如下: #include <iostream> #include <vector> #in ...
- python函数的作用是_Python函数一
函数 一.函数的作用 函数就是一段具有独立功能的代码块整合到一个整体并命名,在需要的位置调用这个名称即可完成对应的需求 函数在开发过程中,可以更高效的实现代码重用 二,函数的使用步骤 2.1 定义函数 ...
- python中匿名函数的作用_Python 中的匿名函数,你会用吗
原标题:Python 中的匿名函数,你会用吗 概念 我们从一个例子引入. 这里有一个元素为非空字符串的列表,按字符串最后一个字母将列表进行排序.如果原列表是 ['abc', 'g', 'def'],则 ...
- java中的string函数_java中string.trim()函数的作用实例及源码
trim()的作用:去掉字符串首尾的空格. public static void main(String arg[]){ String a=" hello world "; Str ...
- C语言中内联函数的作用 inline
C语言中内联函数的作用 inline C语言中内联函数到底有什么作用? 试想一下,每当我们在假设就在主函数中调用另外一个函数的时候,那么这个函数就要入栈或者出栈,比如说下面的一个例子: 点击(此处)折 ...
- python主函数的作用_python中main函数的用法
原博文 2020-03-27 20:25 − **什么场景下会有main函数?** 当该python脚本被作为模块(module)引入(import)时,其中的main()函数将不会被执行. **ma ...
- 【Android 逆向】ptrace 函数 ( C 标准库 ptrace 函数简介 | ptrace 函数真实作用 )
文章目录 一.C 标准库 ptrace 函数简介 二.ptrace 函数真实作用 一.C 标准库 ptrace 函数简介 ptrace 函数 : 在 C 标准库 中有一个 ptrace 函数 , 该函 ...
- STM32中NVIC_SystemReset()函数的作用?什么时候用?
STM32中NVIC_SystemReset()函数的作用?什么时候用? STM32软件复位有两种方式 (1)方式一:NVIC_SystemReset()函数用来复位STM32. 注意1:从SYSRE ...
最新文章
- SAP启用检查双重Invoice功能
- OpenGL ES之GLKit索引绘制“金字塔”及渲染纹理和颜色混合
- SqueezeNet网络模型详解
- SDNU 1469.校门外的树(水题)
- .NET Core使用FluentEmail发送邮件
- design php 如何使用ant_Ant Design Pro初探—添加面包屑
- android studio moudel,Android Studio将module变为library
- 3_03 蛇形填数(数组)
- model 创建帖子的方法
- linux查找有用日志常用技巧
- JAVA程序员面试32问(价值8k)你认同吗?
- 今天分享的案例是关于某电商店铺的年终销售业绩
- 谷歌金山词霸/搜狗、QQ、google输入法模式分析及展望
- 接口测试常用文档模板介绍
- 微软搜索战略全盘革新 计划数月内发布“Kiev”
- 用简单的c程序编写的扫雷!
- DES加密算法的C++实现
- alxc tool 报错数组超出了界限_Stack Overflow上59万浏览量的提问:为什么会发生数组越界?
- 安装和使用所见即所得WYSIWYG的 Web 创作软件 BlueGriffon
- 数字图像隐写术之JPEG 隐写分析