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=[[[Student alloc]init]autorelease]; //直接訪問公共變量 int age= stu->age; //調用set方法 stu.no=25; NSLog(@"age=%i",age); |
總結:點語法都是在調用對象的方法,而不是直接訪問成員變量。