Skip to content

Instantly share code, notes, and snippets.

View markd2's full-sized avatar

Mark Dalrymple markd2

View GitHub Profile
@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 / 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";
//
// 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 / 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
#import <Foundation/Foundation.h>
// clang -g -Weverything -framework Foundation -o count count.m
@interface CountTest : NSObject
- (NSUInteger) count; // notice not a @property
@end
#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
@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