Skip to content

Instantly share code, notes, and snippets.

View krzysztofzablocki's full-sized avatar

Krzysztof Zabłocki krzysztofzablocki

View GitHub Profile
@krzysztofzablocki
krzysztofzablocki / gist:11252222
Created April 24, 2014 12:04
Don't repeat yourself
- (void)viewWillAppear:(BOOL)animated
{
[UIView animateWithDuration:animated ? 0.25f : 0.0f animations:^
{
// Some manipulation
} completion:^
{
// Some completion action
}]
}
@krzysztofzablocki
krzysztofzablocki / Bind lifetime
Created April 22, 2014 13:43
Binding lifetime of self to specific object
- (void)bindLifetimeToObject:(id)object
{
objc_setAssociatedObject(object, (__bridge void *)self, self, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
- (void)releaseLifetimeFromObject:(id)object
{
objc_setAssociatedObject(object, (__bridge void *)self, nil, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}
#!/usr/bin/env ruby
require 'net/http'
require 'openssl'
# Track public available information of a twitter user like follower, follower
# and tweet count by scraping the user profile page.
# Config
# ------
twitter_username = ENV['TWITTER_USERNAME'] || 'foobugs'
@krzysztofzablocki
krzysztofzablocki / gist:8913213
Last active January 25, 2018 22:06
Diet setup for cutting, paste into calca.io and modify stats.
# For training rest 5 min on compounds, 3-5 minutes on smaller ones
# Training A (first set to failure, second set -10% weight + 1 rep):
# Deadlift - 2x4-5
# Overhead Press - 1x6-8
# Weighted Chinup - 2x4-6
# Chest-Supported Rows - 2x6-8
# Close-grip chinup - 1x6-10
# Training B (first set to failure, second set -10% weight + 1 rep):
#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 / gist:4396302
Last active November 24, 2021 19:17
Set symbol breakpoint on objc_msgSend then setup this debug command to log all methods called in iOS Simulator. If you want to do device debugging change esp+4 register to r0, esp+8 to r1 Found long ago somewhere on stackoverflow.
expr -- (void)printf("[%s, %s]\n",(char *) object_getClassName(*(long*)($esp+4)), (char *) *(long *)($esp+8) )
@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);