Skip to content

Instantly share code, notes, and snippets.

@janodev
janodev / puke.m
Created September 8, 2011 07:47
Describing an object using introspection
+(NSString*) describe:(id) object {
NSMutableString *string = [NSMutableString stringWithString:@""];
unsigned int propertyCount;
objc_property_t *properties = class_copyPropertyList([object class], &propertyCount);
for (unsigned int i = 0; i < propertyCount; i++)
{
NSString *selector = [NSString stringWithCString:property_getName(properties[i])
encoding:NSUTF8StringEncoding] ;
@janodev
janodev / gist:1290641
Created October 16, 2011 08:01
Prevent the keyboard covering the view
/**
* Register keyboard notifications.
*/
-(void) registerKeyboardNotifications {
trace(@"register keyboard notifications");
NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
[center addObserver:self
selector:@selector(keyboardDidShow:)
name:UIKeyboardWillShowNotification object:nil];
[center addObserver:self
@janodev
janodev / gist:1980044
Created March 5, 2012 18:11
O(n) performance types
Needs["PlotLegends`"]
Plot[{1, Log[n], n, n*Log[n], n^2, n!, 2^n}, {n, 0, 40},
PlotRange -> {0, 200}, PlotLabel -> "O(n) Performance types",
PlotLegend -> {"1", "Log[n]", "n", "n*Log[n]", "n^2", "n!", "2^n"},
LegendShadow -> None, LegendBorder -> None,
LegendPosition -> {0.9, -0.4}]
@janodev
janodev / gist:2983678
Created June 24, 2012 15:26
Asynchronous testing. Runs in CodeRunner.
// TEST ASYNCHRONOUS CODE
// Runs in Code Runner
#import <Foundation/Foundation.h>
@interface Test : NSObject
-(void)test1BooleanFlag;
-(void)test2BooleanFlagAndTimeLimit;
-(void)test3Semaphore;
@janodev
janodev / gist:3043614
Created July 3, 2012 21:55
timer with dispatch source
typedef void (^loop_work_t)(void);
@interface TaxiLoop : NSObject
@property (nonatomic,assign) loop_work_t block;
@property (nonatomic,assign) NSTimeInterval interval;
@property (nonatomic,assign) dispatch_source_t timerSource;
- (dispatch_queue_t)internalQueue
{
static dispatch_once_t onceToken;
static dispatch_queue_t *internalQueue;
dispatch_once(&onceToken, ^{
internalQueue = dispatch_queue_create...
});
return internalQueue;
}
@janodev
janodev / Array.m
Created November 1, 2012 19:59
NSFastEnumeration over a C array crashing with BAD_ACCESS
#import <Foundation/Foundation.h>
@interface Array : NSObject <NSFastEnumeration> {
id __strong *_objs;
NSUInteger _count;
}
@end
@implementation Array
@janodev
janodev / gist:4041078
Created November 8, 2012 19:47
Running code on a specified queue
// queue name
static const char* s_myqueue = "myqueue";
// return the specific queue
dispatch_queue_t my_queue() {
static dispatch_queue_t _q;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
_q = dispatch_queue_create(s_myqueue, 0);
// sets the key/value data for the specified dispatch queue
@janodev
janodev / gist:4175830
Created November 30, 2012 13:50
Dynamic properties
#import <objc/runtime.h>
#import <Foundation/Foundation.h>
@interface Person : NSObject
@property (nonatomic,strong) NSMutableDictionary *properties;
@end
@implementation Person
// Maybe a functional category on NSArray will help.
@interface NSArray(Extension)
-(NSArray*) map:(id(^)(id object))block;
@end
@implementation NSArray(Extension)
-(NSArray*) map:(id(^)(id object))block {
NSParameterAssert(block != NULL);
NSMutableArray *results = [[NSMutableArray alloc] initWithCapacity:[self count]];