Skip to content

Instantly share code, notes, and snippets.

Avatar

Alejandro Ramirez janodev

View GitHub Profile
@janodev
janodev / puke.m
Created Sep 8, 2011
Describing an object using introspection
View puke.m
+(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 Oct 16, 2011
Prevent the keyboard covering the view
View gist:1290641
/**
* 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 Mar 5, 2012
O(n) performance types
View gist:1980044
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 Jun 24, 2012
Asynchronous testing. Runs in CodeRunner.
View gist:2983678
// 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 Jul 3, 2012
timer with dispatch source
View gist:3043614
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;
View gist:3649261
- (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 Nov 1, 2012
NSFastEnumeration over a C array crashing with BAD_ACCESS
View Array.m
#import <Foundation/Foundation.h>
@interface Array : NSObject <NSFastEnumeration> {
id __strong *_objs;
NSUInteger _count;
}
@end
@implementation Array
@janodev
janodev / gist:4041078
Created Nov 8, 2012
Running code on a specified queue
View gist:4041078
// 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 Nov 30, 2012
Dynamic properties
View gist:4175830
#import <objc/runtime.h>
#import <Foundation/Foundation.h>
@interface Person : NSObject
@property (nonatomic,strong) NSMutableDictionary *properties;
@end
@implementation Person
View gist:4324665
// 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]];