NSArray* stringsLengths( NSArray* strings_ )
{
NSMutableArray* strings_lengths_ =
[ NSMutableArray arrayWithCapacity: [ strings_ count ] ];
for ( NSString* string_ in strings_ )
{
NSNumber* length_ = [ NSNumber numberWithUnsignedInt: [ string_ length ] ];
[ strings_lengths_ addObject: length_ ];
}
return [ NSArray arrayWithArray: strings_lengths_ ];
}
* This source code was highlighted with Source Code Highlighter .
NSArray* stringsLengths( NSArray* strings_ )
{
return [ strings_ map: ^( id string_ )
{
return (id)[ NSNumber numberWithUnsignedInt: [ string_ length ] ];
} ];
}
* This source code was highlighted with Source Code Highlighter .
@ interface Element : NSObject
@property ( nonatomic, retain, readonly ) NSArray* subElements;
@end
* This source code was highlighted with Source Code Highlighter .
You need to create a new array that contains all the elements of all subElements.
NSArray* allSubElements( NSArray* elements_ )
{
NSMutableArray* result_ = [ NSMutableArray array ];
for ( Element* element_ in elements_ )
{
NSArray* object_items_ = element_.subElements;
[ result_ addObjectsFromArray: object_items_ ];
};
return [ NSArray arrayWithArray: result_ ];
}
* This source code was highlighted with Source Code Highlighter .
NSArray* allSubElements( NSArray* elements_ )
{
return [ elements_ flatten: ^( id element_ )
{
return [ element_ subElements ];
} ];
}
* This source code was highlighted with Source Code Highlighter .
[ self beginUpdates ];
[ self.tableView deleteRowsAtIndexPaths: delete_index_pathes_
withRowAnimation: UITableViewRowAnimationBottom ];
// condition_ == true, endUpdates
if ( condition_ )
return ;
[ self.tableView insertRowsAtIndexPaths: insert_index_pathes_
withRowAnimation: UITableViewRowAnimationTop ];
[ self endUpdates ];
* This source code was highlighted with Source Code Highlighter .
@ interface UITableView (BlocksAdditions)
-( void )withinUpdates:( void (^)( void ) )block_;
@end
@implementation UITableView (BlocksAdditions)
-( void )withinUpdates:( void (^)( void ) )block_
{
[ self beginUpdates ];
@ try
{
block_();
}
@ finally
{
[ self endUpdates ];
}
}
@end
* This source code was highlighted with Source Code Highlighter .
[ self.tableView withinUpdates: ^( void )
{
[ self.tableView deleteRowsAtIndexPaths: delete_index_pathes_
withRowAnimation: UITableViewRowAnimationBottom ];
if ( condition_ )
return ;
[ self.tableView insertRowsAtIndexPaths: insert_index_pathes_
withRowAnimation: UITableViewRowAnimationTop ];
} ];
* This source code was highlighted with Source Code Highlighter .
@ interface NSObject (Scheduler)
-( void )performSelector:( SEL )selector_
timeInterval:( NSTimeInterval )time_interval_
userInfo:( id )user_info_
repeats:( BOOL )repeats_;
@end
* This source code was highlighted with Source Code Highlighter .
//
typedef void (^JFFCancelScheduledBlock) ( void );
//
typedef void (^JFFScheduledBlock) ( JFFCancelScheduledBlock cancel_ );
@ interface JFFScheduler : NSObject
// ""
+(id)scheduler;
// " "
+(id)sharedScheduler;
//
// -
-(JFFCancelScheduledBlock)addBlock:( JFFScheduledBlock )block_
duration:( NSTimeInterval )duration_;
// , dealloc JFFScheduler
-( void )cancelAllScheduledOperations;
@end
* This source code was highlighted with Source Code Highlighter .
-(JFFCancelScheduledBlock)addBlock:( JFFScheduledBlock )block_
duration:( NSTimeInterval )duration_
{
//
JFFSimpleBlockHolder* cancel_block_holder_ = [ JFFSimpleBlockHolder simpleBlockHolder ];
block_ = [ [ block_ copy ] autorelease ];
// block_
// performBlock
void (^schedule_block_) ( void ) = [ [ ^
{
block_( cancel_block_holder_.simpleBlock );
} copy ] autorelease ];
// "target"
__block NSTimer* timer_ = [ NSTimer scheduledTimerWithTimeInterval: duration_
target: schedule_block_
selector: @selector( performBlock )
userInfo: nil
repeats: YES ];
__block NSObject* cancel_ptr_ = nil;
__block JFFScheduler* scheduler_ = self;
//
cancel_block_holder_.simpleBlock = ^
{
if ( scheduler_ )
{
[ timer_ invalidate ];
//
[ scheduler_.cancelBlocks removeObject: cancel_ptr_ ];
scheduler_ = nil;
}
};
cancel_ptr_ = (id)cancel_block_holder_.simpleBlock;
// dealloc
[ self.cancelBlocks addObject: cancel_ptr_ ];
return cancel_block_holder_.simpleBlock;
}
* This source code was highlighted with Source Code Highlighter .
@implementation NSObject (PerformBlock)
//
-( void )performBlock
{
void * self_ = self;
JFFSimpleBlock block_ = (JFFSimpleBlock)self_;
block_();
}
@end
//
[ ^ {
NSLog( @"test" );
} performBlock ];
* This source code was highlighted with Source Code Highlighter .
NSLog( @"num of \":\" - %d" , [ @":test:" numberOfCharacterFromString: @":" ] );
* This source code was highlighted with Source Code Highlighter .
NSObject* object_ = [ [ NSObject alloc ] init ];
[ object_ addOnDeallocBlock: ^
{
NSLog( @"test" );
} ];
// - test
[ object_ release ];
* This source code was highlighted with Source Code Highlighter .
-( void )performSelector:( SEL )selector_
timeInterval:( NSTimeInterval )time_interval_
userInfo:( id )user_info_
repeats:( BOOL )repeats_
{
//
NSString* selector_string_ = NSStringFromSelector( selector_ );
NSUInteger num_of_args_ = [ selector_string_ numberOfCharacterFromString: @":" ];
NSString* assert_warning_ = [ NSString stringWithFormat: @"selector \"%@\" should has 0 or 1 parameters" , selector_string_ ];
NSAssert( num_of_args_ == 0 || num_of_args_ == 1, assert_warning_ );
// - __block self_ self
__block id self_ = self;
// scheduled ,
JFFScheduledBlock block_ = ^( JFFCancelScheduledBlock cancel_ )
{
//
if ( !repeats_ )
{
[ self_ removeOnDeallocBlock: cancel_ ];
cancel_();
}
//
num_of_args_ == 1
? objc_msgSend( self_, selector_, user_info_ )
: objc_msgSend( self_, selector_ );
};
JFFScheduler* scheduler_ = [ JFFScheduler sharedScheduler ];
//
JFFCancelScheduledBlock cancel_ = [ scheduler_ addBlock: block_
duration: time_interval_ ];
// scheduled self
[ self addOnDeallocBlock: cancel_ ];
}
* This source code was highlighted with Source Code Highlighter .
SomeClass* object_ = [ [ SomeClass alloc ] init ];
// print
[ object_ performSelector: @selector( print )
timeInterval: 1.
userInfo: nil
repeats: NO ];
// release print ,
// object_
[ object_ release ];
* This source code was highlighted with Source Code Highlighter .
Source: https://habr.com/ru/post/120869/