Skip to content

Instantly share code, notes, and snippets.

View markd2's full-sized avatar

Mark Dalrymple markd2

View GitHub Profile
//
// NSObject+setValuesForKeysWithJSONDictionary.h
// SafeSetDemo
//
// Created by Tom Harrington on 12/29/11.
// Copyright (c) 2011 Atomic Bird, LLC. All rights reserved.
//
#import <Foundation/Foundation.h>
@markd2
markd2 / endian.m
Created August 30, 2012 18:10
Explore endianness.
#import <Foundation/Foundation.h>
// Xcode_3_2_6/usr/bin/gcc -arch ppc -std=c99 -isysroot /Xcode_3_2_6/SDKs/MacOSX10.5.sdk -g -Wall -framework Foundation -o endian-ppc ~/Downloads/endian.m (thanks to Jeremy W. Sherman for the assist)
// clang -arch x86_64 -g -Weverything -framework Foundation -o endian endian.m
int main (void) {
unsigned int values[] = { 1, 387, 8533937 };
@markd2
markd2 / BlahView.m
Created September 17, 2012 20:07
A simple UIView that draws itself in a color based on its address in memory.
@interface BlahView : UIView
@end
@implementation BlahView
- (void) drawRect: (CGRect) rect {
CGRect bounds = self.bounds;
UIColor *color = [UIColor colorWithRed: (((int)self) & 0xFF) / 255.0
@markd2
markd2 / unsafe.m
Created October 4, 2012 14:25
Play with ARC variable lifetime qualifiers
#import <Foundation/Foundation.h>
// clang -g -fobjc-arc -Wall -framework Foundation -o unsafe unsafe.m
@interface NSThing : NSObject
@end
@implementation NSThing
- (void) dealloc {
@markd2
markd2 / atomic.m
Created October 29, 2012 16:48
Exploring the 'atomic' keyword in properties.
#import <Foundation/Foundation.h>
// clang -g -Wall -framework Foundation -o atomic atomic.m
// gcc -g -Wall -framework Foundation -o atomic atomic.m
@interface Blah : NSObject
@property (assign, NS_NONATOMIC_IOSONLY) NSInteger frobnozzle;
@property (assign, ) NSInteger commaAtEnd;
// @property (, assign) NSInteger commaAtBeginning; // Syntax error
@property (atomic, assign) NSInteger blah; // complains in gcc
@markd2
markd2 / fast-enum-1.m
Created November 1, 2012 15:18
Basics of fast enumeration.
#import <Foundation/Foundation.h>
// clang -g -fobjc-arc -Weverything -framework Foundation -o fast-enum-1 fast-enum-1.m
#pragma clang diagnostic ignored "-Wobjc-missing-property-synthesis"
// --------------------------------------------------
// Pass-through
@interface Musician : NSObject
+ (id) musicianWithName: (NSString *) name instrument: (NSString *) instrument;
@markd2
markd2 / fast-enum.m
Created November 8, 2012 20:52
Fast enumeration example
#import <Foundation/Foundation.h>
// clang -g -fobjc-arc -Weverything -framework Foundation -o fast-enum fast-enum.m
#pragma clang diagnostic ignored "-Wobjc-missing-property-synthesis"
// --------------------------------------------------
// Code to iterate a non-array based collection and use fast
// enumeration's stack buffer
// This is a linked list that holds strings. Pretty uninspired.
#import <Foundation/Foundation.h>
// clang -g -Weverything -framework Foundation -o count count.m
@interface CountTest : NSObject
- (NSUInteger) count; // notice not a @property
@end
@markd2
markd2 / array.m
Created February 21, 2013 14:53
On-demand Objective-C message tracing
#import <Foundation/Foundation.h>
// clang -g -fobjc-arc -framework Foundation -o array array.m
void observeObject (id objectToWatch) {
// Just a stub to give DTrace something to hook in to.
}
int main (void) {
@autoreleasepool {
#import <Foundation/Foundation.h>
#import <mach/mach_time.h> // for mach_absolute_time() and friends
// clang -g -fobjc-arc -Weverything -Wno-unused-parameter -framework Foundation -o iteration iteration.m
// Run this by passing arguments on the command line. Run without any
// arguments to see the supported flags. Each time a flag is used causes
// that test to be run and timed, with the time (in seconds) output when
// it finishes. Be careful not to do anything else on your machine (such
// as surfing Redding while getting bored looking at the terminal) otherwise