typedef int (^MyBlock)( int );
int multiplier = 7;
MyBlock myBlock = ^( int num) {
return num * multiplier;
};
* This source code was highlighted with Source Code Highlighter .
int multiplier = 7;
int (^myBlock)( int ) = ^( int num) {
return num * multiplier;
};
* This source code was highlighted with Source Code Highlighter .
myBlock( 3 )
* This source code was highlighted with Source Code Highlighter .
int multiplier = 7;
int (^myBlock)( int ) = ^( int num) {
return num * multiplier;
};
multiplier = 8;
NSLog( @"%d" , myBlock( 3 ) );
* This source code was highlighted with Source Code Highlighter .
__block int multiplier = 7;
int (^myBlock)( int ) = ^( int num) {
return num * multiplier;
};
multiplier = 8;
NSLog( @"%d" , myBlock( 3 ) );
* This source code was highlighted with Source Code Highlighter .
NSDate* date = [ [ NSDate alloc ] init ];
void (^printDate)() = ^() {
NSLog( @"date: %@" , date );
};
//
printDate = [ [ printDate copy ] autorelease ];
[ date release ];
printDate();
* This source code was highlighted with Source Code Highlighter .
NSDate* date = [ [ NSDate alloc ] init ];
void (^printDate)() = ^() {
NSLog( @"date: %@" , date );
};
[ date release ];
//
printDate = [ [ printDate copy ] autorelease ];
printDate();
* This source code was highlighted with Source Code Highlighter .
__block NSDate* date = [ [ NSDate alloc ] init ];
void (^printDate)() = ^() {
// date
NSLog( @"date: %@" , date );
};
// , date retain
printDate = [ [ printDate copy ] autorelease ];
[ date release ];
printDate();
* This source code was highlighted with Source Code Highlighter .
@ interface SomeClass : NSObject
//
@property ( nonatomic, copy ) SimpleBlock block;
@end
@implementation SomeClass
@synthesize block = _block;
-( void )dealloc
{
[ _block release ];
[ super dealloc ];
}
-( void )methodB
{
}
-( void )methodA
{
__block SomeClass* self_ = self;
// ( ) - ,
self.block = ^()
{
// retain self_
[ self_ methodB ];
};
}
@end
* This source code was highlighted with Source Code Highlighter .
@implementation NSObject (BlocksExtensions)
-( void )callSelfBlock
{
void * self_ = self;
ESSimpleBlock block_ = (ESSimpleBlock)self_;
block_();
}
-( void )performAfterDelay:( NSTimeInterval )delay_
{
[ self performSelector: @selector( callSelfBlock ) withObject: nil afterDelay: delay_ ];
}
@end
* This source code was highlighted with Source Code Highlighter .
NSDate* date = [ NSDate date ];
void (^printDate)() = ^() {
NSLog( @"date: %@" , date );
};
[ printDate performAfterDelay: 0.3 ];
* This source code was highlighted with Source Code Highlighter .
void (^printDate)() = ^() {
NSLog( @"date: %@" , [ NSDate date ] );
};
[ printDate performAfterDelay: 0.3 ];
* This source code was highlighted with Source Code Highlighter .
@implementation NSObject (BlocksExtensions)
-( void )callSelfBlock
{
void * self_ = self;
ESSimpleBlock block_ = (ESSimpleBlock)self_;
block_();
}
-( void )performAfterDelay:( NSTimeInterval )delay_
{
// , - afterDelay:
self = [ [ self copy ] autorelease ];
[ self performSelector: @selector( callSelfBlock ) withObject: nil afterDelay: delay_ ];
}
@end
* This source code was highlighted with Source Code Highlighter .
Source: https://habr.com/ru/post/119877/