Skip to content

Instantly share code, notes, and snippets.

View simonwhitaker's full-sized avatar

Simon Whitaker simonwhitaker

View GitHub Profile
@simonwhitaker
simonwhitaker / one-simple-block-trick.markdown
Last active August 29, 2015 13:57
Wrapping a method implementation

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) {
#!/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 August 29, 2015 14:02
A demonstration of how you might use the decorate-sort-undecorate pattern in Swift, using a handful of cool Swift language features.
/*
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

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 }
}
#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";
// 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:
let opts: NSJSONReadingOptions = NSJSONReadingOptions(
rawValue: NSJSONReadingOptions.MutableContainers.rawValue
| NSJSONReadingOptions.MutableLeaves.rawValue
)
@simonwhitaker
simonwhitaker / gist:506429
Created August 3, 2010 14:11
Append the current SVN revision to an iPhone app's version number at build time
# 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
- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation
{
if (UIInterfaceOrientationIsLandscape(fromInterfaceOrientation)) {
// Fade in instructions
[self setInstructionsOpacity:1.0 overTime:0.5];
}
}
@simonwhitaker
simonwhitaker / index.yaml
Created October 20, 2010 12:26
nanoc config for the Goo Software website homepage
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.