Skip to content

Instantly share code, notes, and snippets.

View simonwhitaker's full-sized avatar

Simon Whitaker simonwhitaker

View GitHub Profile

Can't play videos in Vimeo for iOS

I'm unable to play videos in the app. I believe this is due to a known issue with NSDateFormatter when used to parse/format dates for machine-to-machine communication without setting a locale on the date formatter instance. This affects a number of locales, including the UK (but only when the phone is set to use the 12-hour clock); we see these kind of issues a lot!

Steps to reproduce

Note: you must use a physical iPhone – you can't repro this in the simulator.

  1. Set your phone's locale to UK: Settings > General > Language and Region, set Region to United Kingdom
  2. Set your phone to use the 12-hour clock: Settings > General > Date & Time, set 24-Hour Time to OFF
let opts: NSJSONReadingOptions = NSJSONReadingOptions(
rawValue: NSJSONReadingOptions.MutableContainers.rawValue
| NSJSONReadingOptions.MutableLeaves.rawValue
)
// 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:
#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
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
#!/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 / 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) {
@simonwhitaker
simonwhitaker / swmath.m
Last active January 1, 2016 22:58
Architecture-independent floor() function
/*
* 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.
$ 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);