ios學習筆記之block在ios開發中的應用,ios學習筆記
一、什麼是Blocks
Block是一個C層級的文法以及運行時的一個特性,和標準C中的函數(函數指標)類似,但是其運行需要編譯器和運行時支援,從ios4.0開始就很好的支援Block。
二、在ios開發中,什麼情況下使用Block
Block除了能夠定義參數列表、傳回型別外,還能夠擷取被定義時的詞法範圍內的狀態(比如局部變數),並且在一定條件下(比如使用__block變數)能夠修改這些狀態。此外,這些可修改的狀態在相同詞法範圍內的多個block之間是共用的,即便出了該詞法範圍(比如棧展開,出了範圍),仍可以繼續共用或者修改這些狀態。通常來說,block都是一些簡短程式碼片段的封裝,適用作工作單元,通常用來做並發任務、遍曆、以及回調。
三、block如何申明(對比於c語言中的函數申明)
四、c函數指正和blocks調用
int (*CFunc) (int a) 函數調用
int result = CFunc(10);
int (^BFunc) (int a) 函數調用
int result = BFunc(10);
五、__block 關鍵字
一個Block的內部時可以引用自身範圍外的變數的,包括static變數,extern變數或自由變數(定義一個變數的時候,如果不加儲存修飾符,預設情況下就是自由變數auto,auto變數儲存在stack中的。除了auto之外還存在register,static等儲存修飾符),對於自由變數,在Block中唯讀。在引入block的同時,還引入了一種特殊的__block關鍵字變數儲存修飾符。
六、block的幾個小例子
#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);
}
列印結果如下
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寫的回調例子
1、Dog.h
Java代碼
- #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代碼
- #import "Dog.h"
-
-
- @implementation Dog
- @synthesize ID = _ID;
-
-
- -(id) init
- {
- self = [superinit];
- if (self) {
- //每隔1s調用一次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) {
- //調用從Person傳過來的Blocks
- BarkCallback(self, barkCount);
- }
- }
-
-
-
-
- -(void) setBark:(void (^)(Dog *, int))eachBark
- {
- [BarkCallbackrelease];
- BarkCallback = [eachBark copy];
- }
-
-
- -(void) dealloc
- {
- [BarkCallbackrelease];
- [superdealloc];
- }
- @end
3、Person.h
Java代碼
- #import <Foundation/Foundation.h>
- #import "Dog.h"
-
-
- @interface Person : NSObject
- {
- Dog *_dog;
- }
-
-
- @property (retain) Dog *dog;
-
-
- @end
4、Person.m
#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代碼
- #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、列印結果
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