iOS函数式和链式编程优缺点(OC + block)

假如当前iOS项目开发语言是OC, 那么block肯定使用过

下面是Masonry一个简单的使用:

UIView *testV = [[UIView alloc]init];
[self.view addSubview:testV];
[testV mas_makeConstraints:^(MASConstraintMaker *make) {
    make.top.bottom.left.right.equalTo(self.view);
}];

那么其中

1 make.top.bottom.left.right. 这种写法就是链式编程,也就是通过点语法连接,像一条链子一样连接在一起.
2 XX.equalTo(self.view) 这种写法就是函数式编程, 一般需要参数这个就是函数式.

1 一般使用UIButton的方法:

UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
[button setTitle:@"我是按钮!" forState:UIControlStateNormal];
[button setImage:ImageName(placeholderimage) forState:UIControlStateNormal];
[button setBackgroundImage:ImageName(placeholderimage) forState:UIControlStateNormal];
[button addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];

点击事件:

- (void)click:(UIButton *)sender {
    DLog(@"点击了我!");
}

2 仿照Masonry我写一个UIButton的Category :

UIButton+Easy (实现代码写在文章最后),最终实现上述功能效果:

UIButton *button = UIBtn().e_title(@"我是按钮!")
.e_imageBg(ImageName(placeholderimage))
.e_image(ImageName(placeholderimage))
.e_click(^{
     DLog(@"点击了我!");
});

可以发现代码变得简单明了了.

优点:

上面简便的写法就是很实在的优点了!

缺点:

1 不会自动语法提示
对于通过点语法使用Xcode并没有自动语法提示, 那么需要自己添加了.

创建代码块create code snippet:

比如.e_image 方法


图片.png

这样使用.e_image时就能自动提示了,其他同类.
(有更好的办法请留言 !)

同时,进入文件位置: ~/资源库/Developer/Xcode/UserData/CodeSnippets 可以把对应的代码块拷贝到其他电脑上使用!

2 block 比较耗时

一般来说,封装的代码总是比基本语法耗时(代码量大了), OC再快也没有c快,更没有汇编快, 但是大众开发语言只会越来越高级, 因为设备性能越来越好了

通过for循环对上述代码分别循环100遍(一个500行左右的文件).

CFAbsoluteTime start = CFAbsoluteTimeGetCurrent();
for (int i = 0; i < 100; i++) {
    xxx
    xxx
    xxx
}
CFAbsoluteTime end = CFAbsoluteTimeGetCurrent();
    DLog(@"%@",[NSString stringWithFormat:@"block耗时:%0.6f", end - start]);
    

最终打印结果:

2019-03-06 10:04:40.8... 
[Line 329] block耗时:0.994888

2019-03-06 10:04:40.8...
[Line 339] 常规耗时:0.001091

不用我解释了吧, 这耗时比很明显了!
当然实际项目中可能没有这么高的比例 , 但是当你大量运用时也不小啊 !

😔 有人会想, 我几年工作经验的代码, 还没有刚出来的新手的代码跑得快, 😢 !

个人观点:
既要提高开发效率, 同时也要保证APP运行速度, 所以block的使用要量力而行, 不能太泛滥!

3 block 要注意崩溃 和 内存泄漏
这个应该都知道.

(1) 崩溃问题 :

//判断是否为nil
if (block) {
     block(xx);
}
或者
!block ? : block(xx);

(2) 内存泄漏问题 :
内存泄漏

//内存泄漏:
btn.e_click(^{
    [self.xx xx];
    DLog(@"点击了我!");
});

解决

@weakify(self)
btn.e_click(^{
    @strongify(self)
    [self.xx xx];
    DLog(@"点击了我!");
});

最后代码 供参考:

UIButton+Easy.h

@interface UIButton (Easy)
UIButton * UIBtn();
- (UIButton * (^)(NSString *title))e_title;
- (UIButton * (^)(UIImage *image))e_image;
- (UIButton * (^)(UIImage *imageBg))e_imageBg;
- (UIButton * (^)(UIColor *colorBg))e_colorBg;
- (UIButton * (^)(UIColor *colorT))e_colorT;
- (UIButton * (^)(NSInteger font))e_font;
- (UIButton * (^)(NSInteger fontB))e_fontB;

- (UIButton * (^)(NSInteger radius))e_radius;
- (UIButton * (^)(NSInteger borderWidth))e_borderWidth;
- (UIButton * (^)(UIColor *borderColor))e_borderColor;
- (UIButton * (^)(void (^)(void)))e_click;
@end

UIButton+Easy.m

#import "UIButton+Easy.h"

@implementation UIButton (Easy)

- (UIButton *(^)(NSString *))e_title {
    return ^(NSString *text) {
        [self setTitle:StrValue(text) forState:UIControlStateNormal];
        return self;
    };
}

- (UIButton * (^)(UIImage *image))e_image {
    return ^(UIImage *image) {
        [self setImage:image forState:UIControlStateNormal];
        return self;
    };
}
- (UIButton * (^)(UIImage *imageBg))e_imageBg {
    return ^(UIImage *image) {
        [self setBackgroundImage:image forState:UIControlStateNormal];
        return self;
    };
}
- (UIButton * (^)(UIColor *colorBg))e_colorBg {
    return ^(UIColor *color) {
        if (color) {
            [self setBackgroundColor:color];
        }
        return self;
    };
}

- (UIButton * (^)(UIColor *colorT))e_colorT {
    return ^(UIColor *color) {
        if (color) {
            [self  setTitleColor:color forState:UIControlStateNormal];
        }
        return self;
    };
}

- (UIButton * (^)(NSInteger font))e_font {
    return ^(NSInteger font) {
        if (font >= 0) {
            [self.titleLabel setFont:[UIFont systemFontOfSize:font]];
        }
        return self;
    };
}

- (UIButton * (^)(NSInteger fontB))e_fontB {
    return ^(NSInteger font) {
        if (font >= 0) {
            [self.titleLabel setFont:[UIFont boldSystemFontOfSize:font]];
        }
        return self;
    };
}

- (UIButton * (^)(NSInteger radius))e_radius {
    return ^(NSInteger radius) {
        if (radius >= 0) {
            self.layer.cornerRadius = radius;
            self.layer.masksToBounds = YES;
        }
        return self;
    };
}
- (UIButton * (^)(NSInteger borderWidth))e_borderWidth {
    return ^(NSInteger borderWidth) {
        if (borderWidth >= 0) {
            if (borderWidth) {
                self.layer.borderWidth = borderWidth;
            }
        }
        return self;
    };
}
- (UIButton * (^)(UIColor *borderColor))e_borderColor {
    return ^(UIColor *borderColor) {
        if (borderColor) {
            if (borderColor) {
                self.layer.borderColor = borderColor.CGColor;
            }
        }
        return self;
    };
}

/**
 :点击事件
 */
- (UIButton * (^)(void (^)(void)))e_click {
    UIButton *(^clickBlock)(void (^)(void)) = ^(void (^backBlock)(void)) {
        [self bk_whenTapped:backBlock];
        if (backBlock) {
            backBlock();
        }
        return self;
    };
    return clickBlock;
}

@end
UIButton * UIBtn() {
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    return button;
}
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 14,611评论 4 61
  • Swift1> Swift和OC的区别1.1> Swift没有地址/指针的概念1.2> 泛型1.3> 类型严谨 对...
    cosWriter阅读 13,884评论 1 32
  • 2017年10月18日,放学回到家做作业,关于作业的书一本都没拿,跟老师沟通了,我骑车带她赶紧去学校拿,又回到家做...
    爱笑的丫头_327b阅读 1,378评论 0 0
  • 一直以来,我都很喜欢毕淑敏先生的文字。她的文字干净、柔和、平实而又拥有直指人心的力量。所以当我拿到这本《轻轻走向完...
    山水郎_68f9阅读 11,617评论 1 8
  • 你唉声 你叹气 你在无可奈何的时候叹气 你在无话可说的时候叹气 你在与别人交流的附和中轻轻地叹气 那么均匀 那么和...
    猜猜裁纸阅读 1,715评论 1 0

友情链接更多精彩内容