Skip to content

Instantly share code, notes, and snippets.

View markd2's full-sized avatar

Mark Dalrymple markd2

View GitHub Profile
@markd2
markd2 / addstuff.m
Created July 25, 2013 14:21
Support file for Inside the Bracket part 7, adding some methods to classes at runtime.
// Add some methods with the runtime.
#import <Foundation/Foundation.h>
#import <objc/runtime.h>
// clang -g -fobjc-arc -Wall -framework Foundation -o addstuff addstuff.m
@interface Glorn : NSObject
@end
@markd2
markd2 / swizzle.m
Created July 25, 2013 14:18
Support file for Inside the Bracket, part 7, the final chapter, part two. This time, it's swizzling.
#import <Foundation/Foundation.h>
#import <objc/runtime.h>
// clang -g -fobjc-arc -Wall -framework Foundation -o swizzle swizzle.m
// Swap two methods
void SwizzleMethod (Class clas, SEL originalSelector, SEL newSelector) {
Method originalMethod =
class_getInstanceMethod (clas, originalSelector);
@markd2
markd2 / NSObject+setValuesForKeysWithJSONDictionary.h
Created July 18, 2013 13:44
Support files for Inside the Bracket Part 6, showing an actual use of the objective-C runtime API.
//
// NSObject+setValuesForKeysWithJSONDictionary.h
//
// Created by Tom Harrington on 12/29/11.
// Tweaked by Mark Dalrymple
//
// Copyright (c) 2011 Atomic Bird, LLC. All rights reserved.
//
#import <Foundation/Foundation.h>
@markd2
markd2 / trssnd.m
Created July 14, 2013 14:41
Getting to the bottom of some odd compiler behavior.
#import <Foundation/Foundation.h>
// clang -g -Wall -framework Foundation -o trssnd trssnd.m
@interface A : NSObject
@property (assign, readonly, nonatomic) NSInteger blah;
@property (assign, nonatomic) NSInteger hasIvar;
@markd2
markd2 / indexedivars.m
Created July 9, 2013 21:00
Example showing the use of accessing the indexed ivars.
#import <Foundation/Foundation.h>
#import <objc/runtime.h>
// This stuff won't work with ARC, so make sure it's turned off.
// clang -g -fno-objc-arc -framework Foundation -o indexedivars indexedivars.m
@interface Stuffage : NSObject {
int _extraCount;
}
@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
@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
#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 / 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
@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"