Ray's playground

 

An Objective-C Primer(Chapter 2 of Objective-C Phrasebook)

#import <Foundation/Foundation.h>

int(^getCounter(void))(void)
{
    __block 
int counter;
    
int(^block)(void)=^(void){ return counter++; };
    
return _Block_copy(block);
}

int main (void
{
    
int (^block)(void= getCounter();
    block();
    block();
    NSCAssert(block() 
== 2@"Block counted incorrectly");
    
int(^block2)(void= getCounter();
    block2(); 
    block2();
    NSCAssert(block2() 
== 2@"Block didn't start from 0");
    NSLog(
@"%d", block2());
    
return 0;
}

posted on 2011-02-25 18:33  Ray Z  阅读(156)  评论(0编辑  收藏  举报

导航