iOS中Block的简单用法

1. Block作为参数,传递执行语句

void Work(void (^dothings)()){
    NSLog(@"eating...");
    dothings();
    NSLog(@"go home...");
}
void WorkDay(int workday){
    void (^workThing)();
    switch (workday) {
        case 1:
            workThing = ^(){
                NSLog(@"报到。。。");
            };
            break;
        case 2:
            workThing = ^(){
                NSLog(@"分析工作内容。。。");
            };
            break;
        case 3:
            workThing= ^(){
                NSLog(@"敲代码。。。");
            };
            break;
        case 4:
            workThing = ^(){
                NSLog(@"晋升产品经理。。。");
            };
            break;
        case 5:
            workThing = ^(){
                NSLog(@"have a happy life。。。");
            };
            break;
        default:
            break;
    }
    Work(workThing);
}
int main(int argc, const char * argv[]) {
    @autoreleasepool {
        for (int i = 1; i<=5;i++)
            WorkDay(i);
    }
    return 0;
}

2. Block作为返回值

typedef void(^NewType1) ();
typedef int(^NewType2) (int a,int b);

NewType1 testType1(){
    NewType1 n1 = ^{
        NSLog(@"this is newtype1...");
    };
    return n1;
}

NewType2 testType2(){

//    NewType2 n2;
//    n2 = ^(int a,int b){
//        return a + b;
//    };
//    return n2;
    return ^(int a,int b){
        return a + b;
    };
}

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        // insert code here...
        NewType1 n1 = testType1();
        n1();
        NewType2 n2 = testType2();
        int a = n2(10,201);
        NSLog(@"%d",a);
    }
    return 0;
}

3. Block 作为类属性

BlockAsPara.h

#import <Foundation/Foundation.h>

typedef void(^BlockType) ();
typedef int(^IntBlock) (int a,int b);
@interface BlockAsPara : NSObject


- (void)test:(BlockType) block;
//- (void)test:(void(^)())block;

- (void)addTest:(int(^)(int ,int))block andInt:(int)a andInt:(int)b;
- (void)multiTest:(int(^)(int ,int )) block andInt:(int)a andInt:(int)b;
@end

BlockAsPara.m

#import "BlockAsPara.h"

@implementation BlockAsPara

//- (void)test:(void (^)())block{
//    if (block != nil) {
//        block();
//    }
//}
- (void)test:(BlockType)block{
    if (block != nil) {
        block();
    }
}

- (void)addTest:(int (^)(int, int))block andInt:(int)a andInt:(int)b{
    if (block != nil) {
        block(a,b);
    }
}
- (void)multiTest:(int (^)(int , int ))block andInt:(int)a andInt:(int)b{

    if (block != nil) {
        NSLog(@"%d * %d = %d",a, b, block(a,b));
    }
}
@end

mian.m

        void (^b3)() = ^(){
            NSLog(@"bbb");
        };
        b3();

        int (^b4)(int a,int b);
        b4 = ^(int a,int b){
            return  a * b;
        };
        [bp multiTest:b4 andInt:2 andInt:3];
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值