Skip to content

Instantly share code, notes, and snippets.

Avatar
👋
Hiring! See my profile for details.

Simon Whitaker simonwhitaker

👋
Hiring! See my profile for details.
View GitHub Profile
@simonwhitaker
simonwhitaker / one-simple-block-trick.markdown
Last active Aug 29, 2015
Wrapping a method implementation
View one-simple-block-trick.markdown

Here's a nice trick for wrapping a method's implementation in a block, for example for logging the return value.

Have you ever written a method with a load of conditionals and return statements...

- (int)statusCode {
    if (foo) {
        return 1;
    }
    if (bar) {
View git-flip-flop.py
#!/usr/bin/env python
#
# git-flip-flop: checks out two or more commits in a periodic cycle
#
# Save on your $PATH as git-flip-flop
#
# EXAMPLES
#
# Check out abc1234, then def5678, then abc1234, etc, with
# the default 2s between each checkout
@simonwhitaker
simonwhitaker / decorate-sort-undecorate.swift
Last active Aug 29, 2015
A demonstration of how you might use the decorate-sort-undecorate pattern in Swift, using a handful of cool Swift language features.
View decorate-sort-undecorate.swift
/*
Decorate-Sort-Undecorate in Swift
In Swift, you get the length of a String by calling the global function
countElements(). However, The Swift Programming Language states:
> The length of a string cannot be calculated without iterating through
> the string to consider each of its characters in turn. If you are
> working with particularly long string values, be aware that the
> countElements function must iterate over the characters within a string
View Extending-numeric-types.md

I was intrigued by this example from The Swift Programming Guide's Extensions section:

extension Double {
    var km: Double { return self * 1_000.0 }
    var m: Double { return self }
    var cm: Double { return self / 100.0 }
    var mm: Double { return self / 1_000.0 }
    var ft: Double { return self / 3.28084 }
}
View bad-year-format.m
#import <Foundation/Foundation.h>
int main(int argc, char *argv[]) {
@autoreleasepool {
NSDateFormatter *goodDateFormatter = [[NSDateFormatter alloc] init];
goodDateFormatter.dateFormat = @"dd-MM-yyyy HH:mm:ss.SSS";
NSDateFormatter *badDateFormatter = [[NSDateFormatter alloc] init];
badDateFormatter.dateFormat = @"dd-MM-YYYY HH:mm:ss.SSS";
View dogshit.swift
let opts: NSJSONReadingOptions = NSJSONReadingOptions(
rawValue: NSJSONReadingOptions.MutableContainers.rawValue
| NSJSONReadingOptions.MutableLeaves.rawValue
)
View json-snippet.swift
// Error: Cannot invoke 'JSONObjectWithData' with an argument list of type '(NSData, options: Int)'
NSJSONSerialization.JSONObjectWithData(data!, options: 0)
// Error: Type 'NSJSONReadingOptions' does not conform to protocol 'NilLiteralConvertible'
NSJSONSerialization.JSONObjectWithData(data!, options: nil)
// This works, but having to pass in 'rawValue' just feels hacky
NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions(rawValue: 0))
// I'd expect to be able to do something like:
@simonwhitaker
simonwhitaker / gist:506429
Created Aug 3, 2010
Append the current SVN revision to an iPhone app's version number at build time
View gist:506429
# Get location of unparsed Info.plist
GS_INFO_PLIST_INPUT=$(basename "$INFOPLIST_FILE" .plist)
# Get location of parsed Info.plist
GS_INFO_PLIST_PATH="$BUILT_PRODUCTS_DIR/$FULL_PRODUCT_NAME/Info"
# Get version number from unparsed Info.plist
GS_VERSION=$(defaults read "$PROJECT_DIR/$GS_INFO_PLIST_INPUT" CFBundleVersion)
# Append local SVN revision number
View gist:546623
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{
if (UIInterfaceOrientationIsLandscape(fromInterfaceOrientation)) {
// Fade in instructions
[self setInstructionsOpacity:1.0 overTime:0.5];
}
}
@simonwhitaker
simonwhitaker / index.yaml
Created Oct 20, 2010
nanoc config for the Goo Software website homepage
View index.yaml
apps:
- name: RSA Vision
slogan: Enlightenment to go
itunes_url: http://itunes.apple.com/gb/app/rsa-vision/id397348011?mt=8
image: /images/apps/rsa-vision.png
description:
- > The RSA Vision app brings to you the latest videos from the
RSA's free public events programme. You can browse by category,
search for videos and compile a playlist of your favourite
videos to watch later.