Skip to content

Instantly share code, notes, and snippets.

View JensAyton's full-sized avatar

Jens Ayton JensAyton

View GitHub Profile
@JensAyton
JensAyton / ClassCrawler.m
Created November 28, 2010 20:24
Generate a GraphViz file showing the hierarchy of Objective-C classes in a given bundle, super- and subclasses of a given class, or all loaded classes. Requires GraphViz for viewing, http://www.pixelglow.com/graphviz/ Hack notice: this is one.
Moved to a repo: https://github.com/Ahruman/ClassCrawler
@JensAyton
JensAyton / BlockActionHack.m
Created September 21, 2010 14:17
Hack to enable the use of block ivars as IBAction targets. Stuffs a bunch of nastiness in NSObject, but transparenty works for classes implemeting actions: create a property of type BlockAction, assign a block to it, and use the property name (with a trai
/*
Hack to enable the use of block ivars as IBAction targets. Stuffs a bunch
of nastiness in NSObject, but transparenty works for classes implemeting
actions: create a property of type BlockAction, assign a block to it,
and use the property name (with a trailing colon) as the target selector.
Did I mention it's a hack? There are two major problems:
* Type safety. It assumes any block property will have the correct
signature. Can be partially worked around when compiling with Clang,
which embeds @encode() strings into blocks, but this is undocumented.
@JensAyton
JensAyton / gist:548432
Created August 24, 2010 22:11
Sketch for multiple return values in Objective-C
// Sketch for multiple return values in Objective-C
// Method declaration:
- (id, NSError *) someMethod:param;
// Desugars to:
typedef struct { id a, NSError *b } __someMethodResult; // Obviously gensym/anonymous in real implementation
- (__someMethodResult) someMethod:param;
// Return type encoding would be "{?=@@}", anonymous struct of two objects.
// Struct handler from an @encode() string parser for a debug printer.
#import <objc/runtime.h> // For @encode syntax constants.
static void DecodeStruct(const char * const encoding, size_t * const cursor, const void ** const buffer, NSMutableString *string)
{
NSCParameterAssert(encoding != NULL && encoding[*cursor] == _C_STRUCT_B && cursor != NULL && buffer != NULL);
#if INCLUDE_STRUCT_NAMES
/*
© 2010 Jens Ayton
This code may be used freely.
*/
uniform float uValue; // Value/brighness component.
const float kPi = 3.141592653589793;
enum
{
kVKC_Shift = 56,
kVKC_Option = 58,
kVKC_Control = 59,
kVKC_rShift = 60, /* Right-hand modifiers; not implemented */
kVKC_rOption = 61,
kVKC_rControl = 62,
kVKC_Command = 55,
}