美文网首页首页投稿(暂停使用,暂停投稿)
iOS Block 弱应用的两种宏定义方式

iOS Block 弱应用的两种宏定义方式

作者: iOS_Job | 来源:发表于2016-08-17 16:25 被阅读1389次

第一种定义方式(推荐)

以下内容摘自 YYKit

  • @weakify 宏定义

        #ifndef weakify
        #if DEBUG
            #if __has_feature(objc_arc)
            #define weakify(object) autoreleasepool{} __weak __typeof__(object) weak##_##object = object;
            #else
            #define weakify(object) autoreleasepool{} __block __typeof__(object) block##_##object = object;
            #endif
        #else
            #if __has_feature(objc_arc)
            #define weakify(object) try{} @finally{} {} __weak __typeof__(object) weak##_##object = object;
            #else
            #define weakify(object) try{} @finally{} {} __block __typeof__(object) block##_##object = object;
            #endif
        #endif
    #endif
    
  • @strongify 宏定义

    #ifndef strongify
            #if DEBUG
                #if __has_feature(objc_arc)
                #define strongify(object) autoreleasepool{} __typeof__(object) object = weak##_##object;
                #else
                #define strongify(object) autoreleasepool{} __typeof__(object) object = block##_##object;
                #endif
            #else
                #if __has_feature(objc_arc)
                #define strongify(object) try{} @finally{} __typeof__(object) object = weak##_##object;
                #else
                #define strongify(object) try{} @finally{} __typeof__(object) object = block##_##object;
                #endif
            #endif
        #endif
    
  • 使用举例

     @weakify(self)
     
     [self doSomething^{
     
        @strongify(self)
        
     if (!self) return;
     ...
     
     }]; 
     
    

注:block 外用 @weakify(self),block内用@strongify(self) ,不要问为什么,就是这么用的😄

第二种定义方式:

  • WeakSelf 宏定义:

    #define WeakSelf(type) __weak typeof(type) weak##type = type
    
  • StrongSelf 宏定义

    #define StrongSelf(type) __strong typeof(type) strong##type = type
    
  • 使用举例

    WeakSelf(self)
     
    [self doSomething^{
         
    StrongSelf(self)
            
    if (!strongself) return;
    ...
         
    }]; 
    

注:使用起来相对麻烦,复制粘贴代码需要改动很多地方


相关文章

  • iOS Block 弱应用的两种宏定义方式

    第一种定义方式(推荐) 以下内容摘自 YYKit @weakify 宏定义 #ifndef weakify ...

  • iOS Block -浅析 文章

    Block 的使用有两种:1.独立Block 。2.内联Block 。 《一》独立Block 使用方式 一、定义一...

  • iOS block和delegate的区别

      block和代理是iOS开发中实现回调的两种方式,本文主要是对两者的应用场景做一下对比。 1.block简介 ...

  • 关于弱符号和弱引用

    宏定义 关于上述宏定义的说明 强符号 弱符号 弱引用

  • IOS NSLog宏定义

    IOS NSLog宏定义 标签(空格分隔): IOS IOS NSLog宏定义 宏定义NSLog方法,不用加";"...

  • iOS block定义方式

    iOS Block定义方式 第一种定义场景 第二种定义场景 第三种种定义场景 第四种种定义场景

  • Block声明的几种写法

    参考链接:关于Block的定义,和作为参数的写法iOS开发-Object-C Block的实现方式Objectiv...

  • iOS开发常用的宏定义

    Objective-C常用宏/*! 字体 */ /*! 颜色宏定义 */ /*! 弱引用宏 */ /*! 输出显示...

  • OC常用宏定义

    测试输出 Log 屏幕相关 系统相关 定义弱引用、强引用 定义警告宏 颜色宏 其他宏

  • Block及循环引用

    解决block的循环引用有两种方式: 1,通过设置__weak,可以将self指针弱引用,达到解除循环引用的作用 ...

网友评论

    本文标题: iOS Block 弱应用的两种宏定义方式

    本文链接:https://www.haomeiwen.com/subject/iibmsttx.html