Skip to content

Instantly share code, notes, and snippets.

View markd2's full-sized avatar

Mark Dalrymple markd2

View GitHub Profile
@markd2
markd2 / spy.d
Created May 17, 2013 01:56
Look at objective-C messages being sent in an application running in the simulator. "Interesting" symbols (configured in the first BEGIN block, or one provided on the command line) will get stack traces dumped. "ignored" symbols are not logged.
#!/usr/sbin/dtrace -s
/* Run like:
% sudo csh
# ./spy.d $PROCESS_ID [$INTERESTING_PROBEPROV]
Prints a line of dashes every 5 seconds to delineate different experiments.
*/
#pragma D option quiet
@markd2
markd2 / old-days.m
Last active December 17, 2015 15:09
Drawing Views, violating the open/closed principle.
#import <stdio.h>
// clang -g -Wall -o old-days old-days.m
typedef struct Rect {
int x, y, w, h;
} Rect;
typedef enum {
@markd2
markd2 / someObject.m
Created May 23, 2013 13:44
Implementation of doSome:for:, ripe for disassembly. Look at the disassembly of main() for the calls to objc_msgSend.
#import <Foundation/Foundation.h>
// clang -g -Wall -framework Foundation -o someObject someObject.m
@interface SomeObject : NSObject
- (void) doSome: (id) stuff for: (id) reasons;
@end // SomeObject
@markd2
markd2 / signature.m
Created June 6, 2013 14:53
Play around with NSMethodSignature and @encode
#import <Foundation/Foundation.h>
// clang -g -Wall -framework Foundation -o signature signature.m
// A class with a vararg, and non-vararg method, to see if the method signatures
// are different. (don't expect them to be, but ya never know...)
@interface VarBork : NSObject
- (id) gronkVar: (float) hoover, ...;
- (id) gronk: (float) hoover;
@markd2
markd2 / invocation.m
Created June 6, 2013 14:54
Bake a NSInvocation
#import <Foundation/Foundation.h>
// Wrap up a call to rangeOfCharacterFromSet:options:range: in an invocation.
// clang -g -Wall -framework Foundation -o invocation invocation.m
// Little utility to dump out a method signature.
static void printSignature (NSMethodSignature *signature) {
NSLog (@"%ld arguments", [signature numberOfArguments]);
for (NSUInteger i = 0; i < [signature numberOfArguments]; i++) {
@markd2
markd2 / imp.m
Created June 6, 2013 14:55
Jump through an IMP
#import <Foundation/Foundation.h>
// Call a method through a cached IMP pointer.
// clang -g -Wall -framework Foundation -o imp imp.m
int main (void) {
@autoreleasepool {
NSString *string = @"Bork";
@markd2
markd2 / gist:5774591
Last active December 18, 2015 11:19
NSUserDefaults and unexpected classes. Make sure #define BREAKS is 1 Then: % ./defaults-death 2013-06-11 15:50:13.811 defaults-death[48089:303] thing is WAFFLE % defaults write defaults-death spoon -string badger % ./defaults-death 2013-06-11 15:51:01.548 defaults-death[48121:303] thing is BADGER % defaults write defaults-death spoon -integer 23…
#import <Foundation/Foundation.h>
// clang -g -Wall -framework Foundation -o defaults-death defaults-death.m
int main (void) {
@autoreleasepool {
NSUserDefaults *defs = [NSUserDefaults standardUserDefaults];
NSDictionary *defaultDefaults = @{
@"spoon" : @"waffle"
@markd2
markd2 / marsupial.m
Last active April 11, 2018 08:26
Support file for "Inside the Bracket, part 4" . This shows fake implementation of a method.
#import <Foundation/Foundation.h>
// clang -g -fobjc-arc -Wall -framework Foundation -o marsupial marsupial.m
@interface NSObject (AllThingsCanDance)
- (void) wombatDance: (int) repetitions;
@end
#import <Foundation/Foundation.h>
// clang++ -DCOVER_METHODS=1 -fobjc-arc -g -ObjC++ -Wall -framework Foundation -o machinery machinery.cpp
// clang++ -DFORWARD_INVOCATION=1-fobjc-arc -g -ObjC++ -Wall -framework Foundation -o machinery machinery.cpp
// clang++ -DFORWARDING_TARGET=1-fobjc-arc -g -ObjC++ -Wall -framework Foundation -o machinery machinery.cpp
/*
#define COVER_METHODS 0
#define FORWARD_INVOCATION 0
#define FORWARDING_TARGET 0
@markd2
markd2 / runtime.m
Created July 9, 2013 20:55
Objective-C runtime metadata dumper.
#import <Foundation/Foundation.h>
#import <objc/runtime.h>
#import "typestring.h"
// clang -g -fobjc-arc -Wall -framework Foundation -o runtime typestring.m runtime.m
// Runtime reference, at least until Apple breaks the link
// http://developer.apple.com/library/ios/#documentation/Cocoa/Reference/ObjCRuntimeRef/Reference/reference.html