Skip to content

Instantly share code, notes, and snippets.

View krzysztofzablocki's full-sized avatar

Krzysztof Zabłocki krzysztofzablocki

View GitHub Profile
#import <Foundation/Foundation.h>
@interface A : NSObject
- (void)doThingWithObject:(id)o completion:(void(^)(void))completionHandler;
@end
@implementation A
- (void)doThingWithObject:(id)o completion:(void(^)(void))completionHandler
{
NSLog(@"object: %@", o);
@krzysztofzablocki
krzysztofzablocki / gist:6711189
Created September 26, 2013 08:03
Multiple weak call
Wrong:
__weak id obj;
dispatch_async(queue, ^() {
[obj doSomething];
[obj doSomethingElse]; <- can turn to nil at any point, should store in strong at beggining of block
[obj doSomethingElse2];
});
Ok:
With 10.8 SDK, `#define OBJC_OLD_DISPATCH_PROTOTYPES 0` at the top of your file or PCH to get errors if you use objc_msgSend without a cast.
@krzysztofzablocki
krzysztofzablocki / Asserts
Created July 3, 2013 18:52
My take on assertions, code sample accompanying blog post at http://merowing.info/2013/07/expanded-use-of-asserts/
typedef NS_ENUM(NSInteger, kErrorCode) {
kErrorCodeInternal = 431432,
};
//! generate error and log it.
extern NSError *pixle_NSErrorMake(NSString *message, NSUInteger code, NSDictionary *aUserInfo, SEL selector);
#define AssertTrueOrReturnBlock(condition, block) do{ NSAssert((condition), @"Invalid condition not satisfying: %s", #condition);\
if(!(condition)) { block(pixle_NSErrorMake([NSString stringWithFormat:@"Invalid condition not satisfying: %s", #condition], kErrorCodeInternal, nil, _cmd)); return;} }while(0)
@krzysztofzablocki
krzysztofzablocki / Safe nil calls in server code
Created December 19, 2012 12:36
Simplified check as nil is fine
#define SafeCall(arg, methodToCall) (arg !=[NSNull null]) ? [arg methodToCall] : nil
int optionalValue = SafeCall([response valueForKey:@"number"], intValue);
@krzysztofzablocki
krzysztofzablocki / gist:3951446
Created October 25, 2012 08:36
Simple crossfade
[UIView transitionWithView:cell.imageView duration:0.25f options:UIViewAnimationOptionTransitionCrossDissolve animations:^{
cell.imageView.image = decompressedThumbnail;
} completion:nil];
@krzysztofzablocki
krzysztofzablocki / gist:3240133
Created August 2, 2012 19:58
Handling dial like rotation on CCNode by using UIPanGestureRecognizer
//! for gesture recognizer support in cocos2d use https://github.com/krzysztofzablocki/CCNode-SFGestureRecognizers
- (void)handlePanGesture:(UIPanGestureRecognizer*)gestureRecognizer
{
CGPoint location = [[CCDirector sharedDirector] convertToGL:[gestureRecognizer locationInView:gestureRecognizer.view]];
static CGPoint oldLocation;
//! this will make sure that oldLocation is initialized
if (gestureRecognizer.state != UIGestureRecognizerStateBegan) {
//! we need to calculate angle difference between previous position and current one in regards to dial center
CGPoint firstDir = ccpSub(oldLocation, dial.position);
@krzysztofzablocki
krzysztofzablocki / Worst programmer ever.m
Created April 2, 2012 12:06
Who writes code like this ? Like Seriously!?
- (void)dealloc {
[percentGender release];
[percentHisto release];
[percentPS release];
[percentSmoke release];
[percentGender dealloc];
[percentHisto dealloc];
[percentPS dealloc];
[percentSmoke dealloc];
percentGender = nil;
CGContextRef bitmapContext = CGBitmapContextCreate(NULL,
rect.size.width,
rect.size.height,
CGImageGetBitsPerComponent(imageRef),
CGImageGetBytesPerRow(imageRef),
CGImageGetColorSpace(imageRef),
kCGImageAlphaPremultipliedFirst | kCGBitmapByteOrder32Little);
@import UIKit;
@interface UINavigationItem (KZAdditions)
@property (nonatomic, copy) NSDictionary *kz_titleTextAttributes;
@end