| Task | Code |
|---|---|
|
Objective-C
Objective-C Block Definition Syntax Master Cheatsheet »
A very good reference: |
//
// TYPEDEFS
//
typedef int (^MyBlockTypeWithArgs)(int, int);
typedef int (^MySimpleBlockType)();
// Obj-C object argument/return types are ok too of course...
//
// LOCAL VARS
//
int (^multiply)(int, int) = ^int(int a, int b) { return a*b; };
int (^multiply)(int, int) = ^(int a, int b) { return a*b; }; // same as above
MyBlockTypeWithArgs multiple = ^(int a, int b) { return a*b; };
MySimpleBlockType doSomething = ^{ [self _doSomething]; };
//
// PROPERTIES (same as local vars)
//
@property (nonatomic, copy) int (^multiply)(int, int);
@property (nonatomic, strong) MyBlockTypeWithArgs callbackBlock;
//
// METHOD ARGUMENTS
//
@interface MyClass
- (void)doItWithBlock:(int(^)(int, int))blockTakingArgs;
- (void)doItWithBlock3:(void (^)())simpleBlock;
- (void)doItWithBlock:(MyBlockTypeWithArgs)block;
@end |
| Objective-C Objective-C Block Properties With or Without a Typedef » | @property (nonatomic, copy) void (^cbPlaybackFinished)(); @property (nonatomic, copy) void (^cbPlaybackDidOccur)(NSUInteger frame, NSTimeInterval time); // Or if you prefer typedef BOOL (^TesterCallbackType)(); //... @property (nonatomic, copy) TesterCallbackType cbAllowPlaybackActionCallback; |
| Objective-C Block arguments for methods in Objective-C (with & without a typedef) » | typedef NSString *(^MyBlockTypeName)(NSInteger, NSString *); @interface MyClass - (void)doItWithBlock:(MyBlockTypeName)block; - (void)doItWithBlock2:(NSString *(^)(NSNumber *, double))blockTakingArgs; - (void)doItWithBlock3:(void (^)())simpleBlock; @end |
| Objective-C Block-based NSNotifications with ‘self’ without creating a circular reference (retain cycle) under ARC » | - (void)setupNotification {
// Prevents a circular reference. Otherwise dealloc will never be called
__weak SWAnalytics *weakSelf = self;
[[NSNotificationCenter defaultCenter]
addObserverForName:UIApplicationDidEnterBackgroundNotification
object:nil
queue:[NSOperationQueue mainQueue]
usingBlock:^(NSNotification *n) {
[weakSelf doSomething];
}];
}
- dealloc
{
[[NSNotificationCenter defaultCenter] removeObserver:self];
} |
| iOS Crossfade Multiple UIViews with Block-based Animations » Don't get too smart with completions. UIView... | // Nothing to do if it's already the active one
if (activeIndex == theIdx)
return;
// Hide immediately all lagging animations and
// fade out the visible one if any
for (int i=0; i |