Skip to content

Instantly share code, notes, and snippets.

Hiring! See my profile for details.

Simon Whitaker simonwhitaker

Hiring! See my profile for details.
View GitHub Profile
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:
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";

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 }
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
#!/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
# Check out abc1234, then def5678, then abc1234, etc, with
# the default 2s between each checkout
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) {
simonwhitaker / swmath.m
Last active Jan 1, 2016
Architecture-independent floor() function
View swmath.m
* Calling math.h functions like `floor` and `floorf` on CGFloat variables
* becomes problematic when compiling the same code for both 32-bit and
* 64-bit platforms, since CGFloat is double on 64-bit, float on 32-bit.
* Hence, if you compile with -Wconversion set, `floorf` will give a warning
* on 64-bit, while `floor` gives a warning on 32-bit.
* Here's a suggested implementation of an architecture-independent `floor`
* function, written using plain old function overloading which is enabled
* using the `__attribute__((overloadable))` Clang language extension.
View gist:7814762
$ cat test.c
#import <stdio.h>
#define NYBBLE 4
int main(int argc, const char * argv[]) {
unsigned long t = (unsigned long)argc;
t = (t << NYBBLE) | (t >> (sizeof(t) * 8 - NYBBLE));
printf("t is now 0x%lx\n", t);
View option-test.m
#import <Foundation/Foundation.h>
typedef NS_OPTIONS(uint32_t, SomeOption) {
SomeOptionNone = 0,
SomeOptionFoo = 1 << 0,
SomeOptionBar = 1 << 1,
int main(int argc, char *argv[]) {
@autoreleasepool {