当前位置: 首页>编程语言>正文

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;
}
#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;
}

https://www.xamrdz.com/lan/5jg1924443.html

相关文章: