国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學(xué)院 > 開發(fā)設(shè)計 > 正文

ios學(xué)習(xí)筆記之block在ios開發(fā)中的應(yīng)用

2019-11-14 19:17:19
字體:
供稿:網(wǎng)友

一、什么是Blocks      Block是一個C級別的語法以及運行時的一個特性,和標準C中的函數(shù)(函數(shù)指針)類似,但是其運行需要編譯器和運行時支持,從ios4.0開始就很好的支持Block。
二、在ios開發(fā)中,什么情況下使用Block      Block除了能夠定義參數(shù)列表、返回類型外,還能夠獲取被定義時的詞法范圍內(nèi)的狀態(tài)(比如局部變量),并且在一定條件下(比如使用__block變量)能夠修改這些狀態(tài)。此外,這些可修改的狀態(tài)在相同詞法范圍內(nèi)的多個block之間是共享的,即便出了該詞法范圍(比如棧展開,出了作用域),仍可以繼續(xù)共享或者修改這些狀態(tài)。通常來說,block都是一些簡短代碼片段的封裝,適用作工作單元,通常用來做并發(fā)任務(wù)、遍歷、以及回調(diào)。
三、block如何申明(對比于c語言中的函數(shù)申明) 四、c函數(shù)指正和blocks調(diào)用      int (*CFunc) (int a) 函數(shù)調(diào)用      int result = CFunc(10);      int (^BFunc)  (int  a)  函數(shù)調(diào)用      int result = BFunc(10);
五、__block  關(guān)鍵字      一個Block的內(nèi)部時可以引用自身作用域外的變量的,包括static變量,extern變量或自由變量(定義一個變量的時候,如果不加存儲修飾符,默認情況下就是自由變量auto,auto變量保存在stack中的。除了auto之外還存在register,static等存儲修飾符),對于自由變量,在Block中只讀的。在引入block的同時,還引入了一種特殊的__block關(guān)鍵字變量存儲修飾符。
六、block的幾個小例子

java代碼 復(fù)制代碼 收藏代碼
  1. #import <Cocoa/Cocoa.h>  
  2.   
  3.   
  4. int main(int argc, char *argv[])  
  5. {  
  6.     @autoreleasepool {  
  7.         NSLog(@"Hello world");  
  8.         void (^myblocks) (void) = NULL;  
  9.         myblocks = ^(void) {  
  10.             NSLog(@"in blocks");  
  11.         };  
  12.         NSLog(@"before myblocks");  
  13.         myblocks();  
  14.         NSLog(@"after myblocks");  
  15.           
  16.           
  17.         int (^myblocks2) (int a, int b) = ^(int a, int b) {  
  18.             int c = a + b;  
  19.             return c;  
  20.         };  
  21.         NSLog(@"before blocks2");  
  22.         int ret = myblocks2(10, 20);  
  23.         NSLog(@"after blocks2 ret %d", ret);  
  24.           
  25.         //此處如果不加__block會報錯  
  26.         __blockint sum = 0;  
  27.         int (^myblocks3) (int a, int b) = ^(int a, int b) {  
  28.             sum = a + b;  
  29.             return3;  
  30.         };  
  31.         myblocks3(20, 30);  
  32.         NSLog(@"sum is %d", sum);  
  33.     }  
  34.     returnNSapplicationMain(argc, (constchar **)argv);  
  35. }  
#import <Cocoa/Cocoa.h>int main(int argc, char *argv[]){    @autoreleasepool {        NSLog(@"Hello world");        void (^myblocks) (void) = NULL;        myblocks = ^(void) {            NSLog(@"in blocks");        };        NSLog(@"before myblocks");        myblocks();        NSLog(@"after myblocks");                        int (^myblocks2) (int a, int b) = ^(int a, int b) {            int c = a + b;            return c;        };        NSLog(@"before blocks2");        int ret = myblocks2(10, 20);        NSLog(@"after blocks2 ret %d", ret);                //此處如果不加__block會報錯        __blockint sum = 0;        int (^myblocks3) (int a, int b) = ^(int a, int b) {            sum = a + b;            return3;        };        myblocks3(20, 30);        NSLog(@"sum is %d", sum);    }    returnNSApplicationMain(argc, (constchar **)argv);}

打印結(jié)果如下 2012-09-03 10:23:20.878 blockTest[407:403] Hello world 2012-09-03 10:23:20.880 blockTest[407:403] before myblocks 2012-09-03 10:23:20.881 blockTest[407:403] in blocks 2012-09-03 10:23:20.881 blockTest[407:403] after myblocks 2012-09-03 10:23:20.882 blockTest[407:403] before blocks2 2012-09-03 10:23:20.882 blockTest[407:403] after blocks2 ret 30 2012-09-03 10:23:20.882 blockTest[407:403] sum is 50
七、block寫的回調(diào)例子 1、Dog.h

Java代碼 復(fù)制代碼 收藏代碼
  1. #import <Foundation/Foundation.h>  
  2.   
  3.   
  4. @interface Dog : NSObject {  
  5.     int _ID;  
  6.     NSTimer *timer;  
  7.     int barkCount;  
  8.       
  9.     //定義一個blocks變量  
  10.     void (^BarkCallback) (Dog *thisDog, int count);  
  11. }  
  12. @PRoperty (assign) int ID;  
  13.   
  14.   
  15. //向外暴露一個接口  
  16. -(void) setBark:( void (^) (Dog *thisDog, int count) ) eachBark;  
  17.   
  18.   
  19. @end  
#import <Foundation/Foundation.h>@interface Dog : NSObject {    int _ID;    NSTimer *timer;    int barkCount;        //定義一個blocks變量    void (^BarkCallback) (Dog *thisDog, int count);}@property (assign) int ID;//向外暴露一個接口-(void) setBark:( void (^) (Dog *thisDog, int count) ) eachBark;@end

2、Dog.m

Java代碼 復(fù)制代碼 收藏代碼
  1. #import "Dog.h"  
  2.   
  3.   
  4. @implementation Dog  
  5. @synthesize ID = _ID;  
  6.   
  7.   
  8. -(id) init  
  9. {  
  10.     self = [superinit];  
  11.     if (self) {  
  12.         //每隔1s調(diào)用一次updateTimer方法  
  13.         timer = [NSTimerscheduledTimerWithTimeInterval:1.0ftarget:selfselector:@selector(updateTimer:) userInfo:nilrepeats:YES];  
  14.           
  15.     }  
  16.     returnself;  
  17. }  
  18.   
  19.   
  20. -(void) updateTimer:(id) arg  
  21. {  
  22.     barkCount ++;  
  23.     NSLog(@"dog %d bark count %d", _ID, barkCount);  
  24.     //向Person對象進行匯報  
  25.     if (BarkCallback) {  
  26.         //調(diào)用從Person傳過來的Blocks  
  27.         BarkCallback(self, barkCount);  
  28.     }  
  29. }  
  30.   
  31.   
  32.   
  33.   
  34. -(void) setBark:(void (^)(Dog *, int))eachBark  
  35. {  
  36.     [BarkCallbackrelease];  
  37.     BarkCallback = [eachBark copy];  
  38. }  
  39.   
  40.   
  41. -(void) dealloc  
  42. {  
  43.     [BarkCallbackrelease];  
  44.     [superdealloc];  
  45. }  
  46. @end  
#import "Dog.h"@implementation Dog@synthesize ID = _ID;-(id) init{    self = [superinit];    if (self) {        //每隔1s調(diào)用一次updateTimer方法        timer = [NSTimerscheduledTimerWithTimeInterval:1.0ftarget:selfselector:@selector(updateTimer:) userInfo:nilrepeats:YES];            }    returnself;}-(void) updateTimer:(id) arg{    barkCount ++;    NSLog(@"dog %d bark count %d", _ID, barkCount);    //向Person對象進行匯報    if (BarkCallback) {        //調(diào)用從Person傳過來的Blocks        BarkCallback(self, barkCount);    }}-(void) setBark:(void (^)(Dog *, int))eachBark{    [BarkCallbackrelease];    BarkCallback = [eachBark copy];}-(void) dealloc{    [BarkCallbackrelease];    [superdealloc];}@end

3、Person.h

Java代碼 復(fù)制代碼 收藏代碼
  1. #import <Foundation/Foundation.h>  
  2. #import "Dog.h"  
  3.   
  4.   
  5. @interface Person : NSObject  
  6. {  
  7.     Dog *_dog;  
  8. }  
  9.   
  10.   
  11. @property (retain) Dog *dog;  
  12.   
  13.   
  14. @end  
#import <Foundation/Foundation.h>#import "Dog.h"@interface Person : NSObject{    Dog *_dog;}@property (retain) Dog *dog;@end

4、Person.m

Java代碼 復(fù)制代碼 收藏代碼
  1. #import "Person.h"  
  2.   
  3.   
  4. @implementation Person  
  5. @synthesize dog = _dog;  
  6.   
  7.   
  8. -(void) setDog:(Dog *)dog  
  9. {  
  10.     if (_dog != dog) {  
  11.         [_dogrelease];  
  12.         _dog = [dog retain];  
  13.           
  14.         [_dogsetBark:^(Dog *thisDog, int count) {  
  15.             NSLog(@"person dog %d count %d", [thisDog ID], count);  
  16.         }];  
  17.     }  
  18. }  
  19.   
  20.   
  21. -(Dog *) dog  
  22. {  
  23.     return_dog;  
  24. }  
  25.   
  26.   
  27. -(void) dealloc  
  28. {  
  29.     self.dog = nil;  
  30.     [superdealloc];  
  31. }  
  32.   
  33.   
  34. @end  
#import "Person.h"@implementation Person@synthesize dog = _dog;-(void) setDog:(Dog *)dog{    if (_dog != dog) {        [_dogrelease];        _dog = [dog retain];                [_dogsetBark:^(Dog *thisDog, int count) {            NSLog(@"person dog %d count %d", [thisDog ID], count);        }];    }}-(Dog *) dog{    return_dog;}-(void) dealloc{    self.dog = nil;    [superdealloc];}@end

5、Main.m

Java代碼 復(fù)制代碼 收藏代碼
  1. #import <Foundation/Foundation.h>  
  2. #import "Person.h"  
  3. #import "Dog.h"  
  4.   
  5.   
  6. int main(int argc, constchar * argv[])  
  7. {  
  8.   
  9.   
  10.     @autoreleasepool {  
  11.           
  12.         // insert code here...  
  13.         NSLog(@"Hello, World!");  
  14.         Person *person = [[Personalloc] init];  
  15.         Dog *dog = [[Dogalloc] init];  
  16.         [dog setID:10];  
  17.         [person setDog:dog];  
  18.         [dog release];  
  19.         while (1) {  
  20.             [[NSRunLoopcurrentRunLoop] run];  
  21.         }  
  22.         [person release];  
  23.           
  24.     }  
  25.     return 0;  
  26. }  
#import <Foundation/Foundation.h>#import "Person.h"#import "Dog.h"int main(int argc, constchar * argv[]){    @autoreleasepool {                // insert code here...        NSLog(@"Hello, World!");        Person *person = [[Personalloc] init];        Dog *dog = [[Dogalloc] init];        [dog setID:10];        [person setDog:dog];        [dog release];        while (1) {            [[NSRunLoopcurrentRunLoop] run];        }        [person release];            }    return 0;}

6、打印結(jié)果 2012-09-03 11:21:08.551 blockDelegate[549:403] Hello, World! 2012-09-03 11:21:09.554 blockDelegate[549:403] dog 10 bark count 1 2012-09-03 11:21:09.555 blockDelegate[549:403] person dog 10 count 1 2012-09-03 11:21:10.554 blockDelegate[549:403] dog 10 bark count 2 2012-09-03 11:21:10.555 blockDelegate[549:403] person dog 10 count 2 2012-09-03 11:21:11.553 blockDelegate[549:403] dog 10 bark count 3 2012-09-03 11:21:11.554 blockDelegate[549:403] person dog 10 count 3 2012-09-03 11:21:12.554 blockDelegate[549:403] dog 10 bark count 4 2012-09-03 11:21:12.555 blockDelegate[549:403] person dog 10 count 4 2012-09-03 11:21:13.553 blockDelegate[549:403] dog 10 bark count 5 2012-09-03 11:21:13.553 blockDelegate[549:403] person dog 10 count 5 2012-09-03 11:21:14.553 blockDelegate[549:403] dog 10 bark count 6 2012-09-03 11:21:14.554 blockDelegate[549:403] person dog 10 count 6


發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 云南省| 肥乡县| 长垣县| 建湖县| 曲麻莱县| 赤壁市| 紫阳县| 大邑县| 洛浦县| 蚌埠市| 保定市| 洪湖市| 循化| 龙川县| 南安市| 新巴尔虎右旗| 武汉市| 呼图壁县| 庆元县| 丘北县| 高邮市| 兴宁市| 类乌齐县| 浙江省| 临海市| 迁安市| 库车县| 洪湖市| 南宫市| 江永县| 府谷县| 兴文县| 宁都县| 通州区| 门源| 勃利县| 哈尔滨市| 三门峡市| 正镶白旗| 滨州市| 东乌|