Skip to content

Instantly share code, notes, and snippets.

View markd2's full-sized avatar

Mark Dalrymple markd2

View GitHub Profile
@markd2
markd2 / BNRDiggyDict.h
Created March 20, 2012 13:45
Objective-C Literals, part 2 support files.
#import <Foundation/Foundation.h>
@interface BNRDiggyDict : NSObject
// React to object indexing. Would be nice to have a @protocol for this
- (id) objectForKeyedSubscript: (id) key;
- (void) setObject: (id) thing forKeyedSubscript: (id<NSCopying>) key;
// And for fun, it also can react to scalar indexing.
// Returns the N'th key of the top-level collection.
@markd2
markd2 / equaltime.m
Created March 26, 2012 00:54
Random comparisons of isEqual:, isEqualToString:, and compare:
#import <Foundation/Foundation.h>
#import <mach/mach_time.h> // for mach_absolute_time() and friends
// clang -g -Wall -framework Foundation -o equaltime equaltime.m
// clang -g -arch i386 -Wall -framework Foundation -o equaltime equaltime.m
CGFloat BNRTimeBlock (void (^block)(void)) {
mach_timebase_info_data_t info;
if (mach_timebase_info(&info) != KERN_SUCCESS) return -1.0;
@markd2
markd2 / timestarts.m
Created March 28, 2012 21:42
Timing code for isEqual: and isEqualToString:, under different scenarios
#import <Foundation/Foundation.h>
#import <mach/mach_time.h> // for mach_absolute_time() and friends
// clang -g -Wall -framework Foundation -o timestarts timestarts.m
CGFloat BNRTimeBlock (void (^block)(void)) {
mach_timebase_info_data_t info;
if (mach_timebase_info(&info) != KERN_SUCCESS) return -1.0;
uint64_t start = mach_absolute_time ();
@markd2
markd2 / scanstop.m
Created April 26, 2012 03:46
Scan/Stop examples
#import "stdio.h"
// clang -g -Wall -o scanstop scanstop.m
static short intArray[] = { 1, 1, 2, 3, 5, 7, 12, 19, 31 };
static char *ravenArray[] = { "once", "upon", "a", "midnight", "dreary" };
typedef struct Groovy {
int value;
@markd2
markd2 / bool.m
Created May 7, 2012 19:14
Playing with different scenarios with Objective-C's BOOL type
#import <Foundation/Foundation.h>
// clang -g -Wall -framework Foundation -o bool bool.m
// Are these two integers different? YES if so, NO if not
// (even though it says YES and NO, it LIES! Can actualy return a range
// of non-YES values)
static BOOL different (int thing1, int thing2) {
@markd2
markd2 / serial.m
Created May 14, 2012 15:06
Playing with NSPropertyListSerialization
#import <Foundation/Foundation.h>
// clang -g -Wall -fobjc-arc -framework Foundation -o serial serial.m
static id makePlistObjects (void) {
NSMutableDictionary *top = [NSMutableDictionary dictionary];
[top setObject: @"Hi I'm a string" forKey: @"string"];
[top setObject: [NSNumber numberWithInt: 23] forKey: @"number"];
@markd2
markd2 / log.m
Created June 25, 2012 15:27
QuietLog, and a little demonstration
#import <Foundation/Foundation.h>
// clang -fobjc-arc -Wall -Wformat -Weverything -Wno-format-nonliteral -framework Foundation -o log log.m
// Compiler likes explicit function prototypes prior to first use.
// Add an attribute to get additional checking from the compiler
extern void QuietLog (NSString *format, ...) __attribute__((format(__NSString__, 1, 2)));
// !!! The attribute above isn't warning like it should, but NSLog's _is_ working.
// This is NSLog's attribute. I'm currently baffled.
#import <Foundation/Foundation.h>
// clang -Weverything -framework Foundation -o string-fun string-fun.m
int main (void) {
@autoreleasepool {
{
// Split and join
NSString *splitString = @"hello:-(there:-(everybody";
@markd2
markd2 / notification.m
Created July 16, 2012 23:15
Spying on notifications.
#import <Cocoa/Cocoa.h>
// clang -Weverything -fobjc-arc -framework Cocoa -o notification notification.m
void QuietLog (NSString *format, ...);
void StartSpying (void);
void StopSpying (void);
void QuietLog (NSString *format, ...) {
@markd2
markd2 / mutant.m
Created August 13, 2012 15:23
Trying to discern a mutable dictionary from an immutable one.
#import <Foundation/Foundation.h>
// clang -g -framework Foundation -o mutant mutant.m
int main (void) {
NSMutableDictionary *mutable = [[NSMutableDictionary alloc] init];
NSDictionary *immutable = [[NSDictionary alloc] init];
NSLog (@"mutable responds to set object for key: %d",
[mutable respondsToSelector: @selector(setObject:forKey:)]);