block和指针函数

来源:互联网 发布:深圳网络推广培训 编辑:程序博客网 时间:2024/05/19 20:45

block代码块和指针函数在定义上只有一个符号的细微差别,至于灵活性和使用场景,具体问题具体分析,block比较灵活。

#import <Foundation/Foundation.h>

#import "Student.h"

int sumAb(int a,int b){

   return a+b;

}

void testBlock(){

    //定义一个block类型

   typedef int (^sum) (int,int);

   //定义了一个指针,指向函数

   typedef int (*sump) (int,int);

    //定义了一个block变量

   sum su=^(int a,int b){

       return a+b;

    };  

   int k=su(10,12);

    NSLog(@"block测试值是%i",k);   

    //指向函数

   sump p=sumAb;

   // int ps=(*p)(12,12);

   int ps=p(25,10);

   NSLog(@"指针函数值%i",ps);

}


访问公共变量:

#import <Foundation/Foundation.h>

@protocol Study;

@protocol Learn;

@interface Student :NSObject<Study,Learn>{

    @public

   int age;

}

@property (nonatomic,assign)int no;

@end


   Student *stu=[[[Studentalloc]init]autorelease];

        //直接访问公共变量

      int age= stu->age;

        //调用set方法

        stu.no=25;

       NSLog(@"age=%i",age);


总结:点语法都是在调用对象的方法,而不是直接访问成员变量。

0 0
原创粉丝点击