• block


    //1 声明block

    //2 实现block

    //3 调用block

    //声明函数指针

    //void (*ptest1) (void)

    //声明block,要指向匿名函数

    void (^block1)(void);

    int (^block2)(int a,int b);

    int (^block3)(int a,int b);

    typedef void (^block4)(void);

    typedef int (^block5)(int a,int b);

    //block的实现

    //block1指向匿名函数,匿名函数结尾要有分号,如果block参数类型为void,那么指向的匿名函数的参数可以省略不写,其他的不可以省略

    //block指向匿名函数的返回值可以省略(所有类型的返回值都可以省略)

    block1=^

    {
    printf("block1 ");

    };

    //匿名函数的作用为求a+b的和

    block2=^(int a,int b)

    {

    return a+b;

    };

    block3=^(int a,int b)

    {
    return a-b;

    };

    //bl4的作用为block4

    block4 bl4;

    bl4=^

    {
    NSLog(@"block4");

    };

    block5 bl5;

    bl5=^(int a,int b)

    {
    return a/b;

    };

    //block的调用

    block1();

    int sum=block2(2,5);

    printf("%i ",sum);

    int mut=block3(5,3);

    printf("%i ",mut);

    bl4();

    NSLog(@"%i",bl5(8,2));

    #################################################

    void test1(void)

    {

        printf("test1 ");

    }

    void test2(void)

    {

        printf("test2 ");

    }

    int test3(int a,int b)

    {

        return a+b;

    }

    int test4(int a,int b)

    {

        return a-b;

    }

    (1)//通过函数名调用C函数

    test1();

    (2)//声明函数指针,函数指针指向test1函数

    //函数指针可以指向函数

    void (*ptest1)(void);

    ptest1=test1;

    //通过指针调用函数

    ptest1();

    void (*ptest2)(void);

    ptest2=test2;

    ptest2();

    //通过函数名调用

    int result=test3(2,5);

    printf("%d",result);

    //通过函数指针调用

    int (*ptest3)(int a,int b);

    ptest3=test3;

    int result=ptest3(2,5);

    printf("%d",result);

    int (*ptest4)(int a,int b);

    ptest4=test4;

    int sum=ptest4(5,2);

    printf("%d",sum);

    //typedef自定义类型

    typedef int MYINT;

    MYINT i=10;

    printf("%d",i);

    typedef void (*tdftest1)(void);

    tdftest1 t1=test1;

    //t1=test1;

    t1();

    typedef int (*tdfTest3)(int a,int b);

    tdfTest3 t3=test3;

    int a=t3(5,2);

    printf("%d",a);

    //实战练习

    在block中调用某些控件会有警告,可以通过下面的方法解除警告

    __block UILabel *label=self.label;

    _secondController.bl=^(float size)

    {
    label.font=[UIFont systemFontOfSize:size];

    };

    #import <UIKit/UIKit.h>

    //声明block

    typedef void(^block)(float size);

    @interface SecondViewController : UIViewController

    - (IBAction)pressSteper:(id)sender;

    //给block封装属性时,需要用strong修饰

    //如果封装block的属性时,那么声明的block 需要用typedef修饰

    @property(strong,nonatomic)block bl;

    @end

  • 相关阅读:
    网页中的JavaScript
    css颜色表示
    css文本属性
    css2选择器
    css3选择器
    Canvas练习
    Canvas
    CSS样式之语法
    css基础1
    php操作成功返回当前页并刷新
  • 原文地址:https://www.cnblogs.com/y16879w/p/4482699.html
Copyright © 2020-2023  润新知