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;
}
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 158,425评论 4 361
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,058评论 1 291
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 108,186评论 0 243
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,848评论 0 204
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,249评论 3 286
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,554评论 1 216
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,830评论 2 312
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,536评论 0 197
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,239评论 1 241
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,505评论 2 244
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,004评论 1 258
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,346评论 2 253
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 32,999评论 3 235
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,060评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,821评论 0 194
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,574评论 2 271
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,480评论 2 267

推荐阅读更多精彩内容

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