Skip to content

Instantly share code, notes, and snippets.

@janodev
janodev / ARSketchView.h
Last active December 22, 2015 19:19
ARSketchView
@interface ARSketchView : UIView
- (IBAction) clear;
- (IBAction) replay;
@end
@janodev
janodev / Person.m
Created June 27, 2013 11:48
Dynamic get/set. Watch it, I'm using [[NSThread currentThread] threadDictionary] for thread-safe access.
#import <objc/runtime.h>
#import <Foundation/Foundation.h>
@interface Person : NSObject
@end
@implementation Person
@end
@interface Person(dynamicProperties)
@janodev
janodev / factorial.m
Created May 25, 2013 20:12
Factorial with recursive blocks.
#import <Foundation/Foundation.h>
// clang -framework Foundation main.m -o main && ./main
int main(int argc, char *argv[]) {
@autoreleasepool {
NSUInteger (^factorial)(NSUInteger n);
NSUInteger (^__block __weak weakFactorial)(NSUInteger n);
weakFactorial = factorial = ^ NSUInteger (NSUInteger n) {
return n==0 ? 1 : n * weakFactorial(n-1);
@janodev
janodev / Person.h
Last active December 17, 2015 17:59
Different ways to execute a block property. This runs in CodeRunner. Replace one of the #ifdef X below with #ifdef YES, and comment person = nil;.
#import <Foundation/Foundation.h>
#undef X
typedef void (^salute_t)();
@interface Person : NSObject
@property (nonatomic,copy) NSString *name;
@property (nonatomic,copy) salute_t salute;
@janodev
janodev / NSTimer+(BlockSupport).m
Last active December 17, 2015 07:48
NSTimer with safe release. Credits to Effective Objective-C 2.0.
#import <Foundation/Foundation.h>
@interface NSTimer (BlockSupport)
+ (NSTimer*)scheduledTimerWithTimeInterval:(NSTimeInterval)interval
block:(void(^)())block
repeats:(BOOL)repeats;
@end
#!/bin/bash
# This script builds the iOS and Mac openSSL libraries
# It's expanded from https://github.com/st3fan/ios-openssl
set -xe
# Setup paths to stuff we need
OPENSSL_VERSION="1.0.1e"
@janodev
janodev / MySingleton.h
Last active July 28, 2016 03:19
Singleton pattern with __atribute__ unavailable to prevent the accidental misuse of a singleton. Note that the user is still able to create more instances using runtime functions.
#import <Foundation/Foundation.h>
@interface MySingleton : NSObject
+(instancetype) sharedInstance;
// clue for improper use (produces compile time error)
+(instancetype) alloc __attribute__((unavailable("alloc not available, call sharedInstance instead")));
-(instancetype) init __attribute__((unavailable("init not available, call sharedInstance instead")));
+(instancetype) new __attribute__((unavailable("new not available, call sharedInstance instead")));
@janodev
janodev / gist:5341598
Created April 8, 2013 23:44
Text to speech on iOS with private framework.
// Not App Store safe. Only available in real devices.
#define RTLD_LAZY 0x1
#define RTLD_NOW 0x2
#define RTLD_LOCAL 0x4
#define RTLD_GLOBAL 0x8
NSObject *voiceSynthesizer;
void *voiceServices;
@janodev
janodev / gist:5332644
Last active December 15, 2015 22:19
Dynamic GCD accessors with resolveInstanceMethod: and dispatch_barrier_async.
#include <objc/runtime.h>
#import <Foundation/Foundation.h>
@interface Hi : NSObject
@end
@implementation Hi {
NSString *something;
}
@janodev
janodev / gist:5292003
Last active December 15, 2015 16:49 — forked from iamleeg/gist:5290797
This creates an Objective-C object in the stack. ARC code.
#import <Foundation/Foundation.h>
#include <stdlib.h>
#include <objc/runtime.h>
@interface A : NSObject
@property (assign) int meaning;
@end
@implementation A