03 Object-C中的category和block

1、category:分类

在java中,如果想要扩展某一个类的,最直接的方法就是采用继承的方式,但是,继承有个最大的弊端就是耦合度实在是太高,在大型项目中,写到后面都有种想死的心都有,只要父类改变了一点点功能,就得去检查是否会影响到子类。所以在实际开发中尽量少用继承这种方式,一般会采用装饰等其他方式来避开继承。
object-c中,设计出了category这个机制,对于扩展类的功能来说,比起继承来说,也算是一个较大的改进了。

2、block

一种数据类型,类似于匿名函数或是javascript中的闭包吧

//
//  Calculate.h
//  IOS_Study_Sample
//
//  Created by luozheng on 2018/1/26.
//  Copyright © 2018年 luozheng. All rights reserved.
//

#import <Foundation/Foundation.h>

@interface Calculate : NSObject

typedef int (^calculateBlock) (int,int);

-(int)calculate:(int)num1 num2:(int)num2 block:(calculateBlock)cal;

@end


//
//  Calculate.m
//  IOS_Study_Sample
//
//  Created by luozheng on 2018/1/26.
//  Copyright © 2018年 luozheng. All rights reserved.
//

#import "Calculate.h"

@implementation Calculate

-(int)calculate:(int)num1 num2:(int)num2 block:(calculateBlock)cal
{
    return cal(num1,num2);
}

@end


//
//  main.m
//  04-block测试
//
//  Created by luozheng on 2018/1/26.
//  Copyright © 2018年 luozheng. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "Calculate.h"

NSString* hello(int i)
{
    //NSLog(@"hello 999999999999999");
    return [NSString stringWithFormat:@"hello 999999999999999 %d",i];
}

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        // myHello是一个指向函数的指针,最前面是返回值,注意返回值不要使用括号
        NSString* (*myHello)(int) = hello;
        NSString* result = myHello(100);
        NSLog(@"result = %@",result);
        
        // block类似于函数指针,唯一的不同就是函数不用在外面单独写了
        NSString* (^myBlock) (int) = ^ NSString* (int i) {
            return [NSString stringWithFormat:@"hello 666666666666 %d",i];
        };
        NSString* blockResult = myBlock(888);
        NSLog(@"blockResult = %@",blockResult);
        
        // block作为参数传给方法
        int (^sumBlcok)(int,int) = ^ int (int num1,int num2){
            return num1 + num2;
        };
        
        Calculate *calculate = [[Calculate alloc] init];
        int sum = [calculate calculate:20 num2:68 block:sumBlcok];
        NSLog(@"sum result = %d",sum);
    }
    return 0;
}


3、protocol:协议,这个直接理解成interface就行。
4、SEL 数据类型:指向方法的一个指针

没有啥可多说的,有个问题看看原因和处理方法就行。
performSelector may cause a leak because its selector is unknown

//
//  main.m
//  05-protocol测试
//
//  Created by luozheng on 2018/1/26.
//  Copyright © 2018年 luozheng. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "Button.h"

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        Button<ViewNameProtocol,ViewLifecycleProtocol> * button = [[Button alloc] init];
        
//        [button onCreate];
//        NSLog(@"Button name = %@",[button name]);
//        [button onDestroy];
        
        SEL s1 = @selector(onCreate);
        [button performSelector:@selector(onCreate)];
        [button performSelector:NSSelectorFromString(@"onCreate")];
    }
    return 0;
}

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • *面试心声:其实这些题本人都没怎么背,但是在上海 两周半 面了大约10家 收到差不多3个offer,总结起来就是把...
    Dove_iOS阅读 27,281评论 30 472
  • 禅与 Objective-C 编程艺术 (Zen and the Art of the Objective-C C...
    GrayLand阅读 1,683评论 1 10
  • runtime 和 runloop 作为一个程序员进阶是必须的,也是非常重要的, 在面试过程中是经常会被问到的, ...
    made_China阅读 1,237评论 0 7
  • 原文(12.8)一曰长目,二曰飞耳,三曰树明。明知千里之外,隐微之中,是谓洞天下奸,莫不暗变更。右主恭。 译文:一...
    我在霾中等风阅读 550评论 0 0
  • 早晨,打开简书,随意翻看首页推送的文章,看了一篇“为什么选择清晨写作”,回答正题,因为早晨是一天中最具有“包容性”...
    元又元阅读 384评论 0 0