samurai-native 学习笔记--samurai中的宏魔法(待更新)

samurai-native 学习笔记–samurai中的宏魔法

文件 Samurai_Predefine.h
Samurai里的一些预设宏

// 这3个宏是在定义属性 @prop_assign( NSInteger, page IN)的时候标记这个数据流的方向时用的.
#define IN
#define OUT
#define INOUT
// 函数属性限定符: __attribute__((unused)) 这个限定符属性禁止编译器在未引用该函数时生成警告
// 这里先用__unused_var__消除__x的未使用警告用的, 然后用__attribute__((unused))消除了__unused_var__的
#define UNUSED( __x )       { id __unused_var__ __attribute__((unused)) = (id)(__x); }
// 定义别名
#define ALIAS( __a, __b )   __typeof__(__a) __b = __a;
// 标记过期
#define DEPRECATED          __attribute__((deprecated))
// 这是一个可以在Issue Navigator中显示todo的宏
// macro_cstr 稍后介绍
#define TODO( X )           _Pragma(macro_cstr(message("✖✖✖✖✖✖✖✖✖✖✖✖✖✖✖✖✖✖ TODO: " X)))
// 强制内联
#define INLINE              __inline__ __attribute__((always_inline))
// arc mrc 兼容处理
#if !defined(__clang__) || __clang_major__ < 3

    #ifndef __bridge
    #define __bridge
    #endif

    #ifndef __bridge_retain
    #define __bridge_retain
    #endif

    #ifndef __bridge_retained
    #define __bridge_retained
    #endif

    #ifndef __autoreleasing
    #define __autoreleasing
    #endif

    #ifndef __strong
    #define __strong
    #endif

    #ifndef __unsafe_unretained
    #define __unsafe_unretained
    #endif

    #ifndef __weak
    #define __weak
    #endif

#endif
#define weakify( x ) \
        _Pragma("clang diagnostic push") \
        _Pragma("clang diagnostic ignored \"-Wshadow\"") \
        autoreleasepool{} __weak __typeof__(x) __weak_##x##__ = x; \
        _Pragma("clang diagnostic pop")

#define strongify( x ) \
        _Pragma("clang diagnostic push") \
        _Pragma("clang diagnostic ignored \"-Wshadow\"") \
        try{} @finally{} __typeof__(x) x = __weak_##x##__; \
        _Pragma("clang diagnostic pop")

// 这段宏是用来消除block里的循环引用的,在block外@weakify( self ),在block内@strongify( self );
// _Pragma用来忽略编译警告
// try{} @finally{} 用来和前面的@生成完整的语句
// __typeof的作用是: 首先定义一个名为 __weak_self__ 类型为 self 同样的类型的 __weak 的变量指向 self, 
然后定义一个名为 self 类型为 self 同样的类型的 __strong的 变量指向 self .这样在block里面我们实际用的 self 其实是一个名字叫 self 的局部变量.
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值