Skip to content

Instantly share code, notes, and snippets.

View ChrisLawther's full-sized avatar

Chris Lawther ChrisLawther

  • Manchester, UK
View GitHub Profile
@ChrisLawther
ChrisLawther / CoordinateConversion.swift
Last active January 29, 2021 21:29
Code for converting British grid references into latitude and longitude
import CoreLocation
// A Swift reimplementation of the Objective-C code listed here :
// http://www.hannahfry.co.uk/blog/2014/12/26/more-on-converting-british-national-grid-to-latitude-and-longitude
extension CLLocation {
convenience init(easting E: Double, northing N: Double) {
// The Airy 180 semi-major and semi-minor axes used for OSGB36 (m)
let (a, b) = (6377563.396, 6356256.909)
// Custom Notifications, in a Swift-y way
// Firstly, to make testing much easier, let's write a protocol to describe what we want
// to be able to do with notifications:
protocol NotificationSource {
func post(_ notification: Notification)
func post(name: Notification.Name, object: Any?)
func post(name: Notification.Name, object: Any?, userInfo: [AnyHashable: Any]?)
func addObserver(forName name: NSNotification.Name?,
// Following on from:
// https://gist.github.com/azureblue75/b28dfb0b12dd0e3a8139a92b9a0243ec
// ...and building on the approaches discussed in:
// https://talk.objc.io/episodes/S01E27-typed-notifications-part-1
// Let's define a protocol for receiving system notifications:
protocol ReceivableNotification {
static var name: Notification.Name { get }
init(notification: Notification)
// Allows reading bytes, 16-bit words, 32-bit words etc. out of the supplied buffer.
// Often useful when decoding data from, for example, a Bluetooth device which produces
// data containing flags to indicate the presence/absence of optional fields.
// See https://codereview.stackexchange.com/q/196429/171827 for it's genesis/evolution
public struct ConsumableByteArray {
private let bytes: [UInt8]
private var idx = 0
@ChrisLawther
ChrisLawther / ChainingAnimator.swift
Last active July 19, 2019 11:50
A cleaner way to write chained UIView animations
// Chaining UIView automations inside completion blocks of earlier animations quickly gets confusing.
// Breaking animations out into their own function can ease this a little.
// Here we take an alternative approach which allows chained animations to be written more naturally.
// There's more repetition than I'd like, to support the two flavours of animation, but it makes for
// a cleaner call site.
// (with or without initial velocity and damping).
//
// (It's probably been done before by others, but XCode 10 just landed and I wanted something to )
// (do in a Playground to see if they're any better behaved. )
// See usage example below
//
// IdentifiedReusableViews.swift
//
// Created by Chris on 29/05/2019.
// Copyright © 2019 Chris Lawther. All rights reserved.
//
// By conforming your UITableView{Cell,HeaderFooterView} subclasses to
// `Identified{Cell,HeaderFooter}`, you gain:
//
// Prompted by an article here: https://fluffy.es/download-files-sequentially/
// ... an alternative, lighter-weight approach to ensuring asynchronous downloads happen sequentially
let urls = [
URL(string: "https://github.com/fluffyes/AppStoreCard/archive/master.zip")!,
URL(string: "https://github.com/fluffyes/currentLocation/archive/master.zip")!,
URL(string: "https://github.com/fluffyes/DispatchQueue/archive/master.zip")!,
URL(string: "https://github.com/fluffyes/dynamicFont/archive/master.zip")!,
URL(string: "https://github.com/fluffyes/telegrammy/archive/master.zip")!
]
#!/usr/bin/env bash
# Run in the top directory of an SPM project to have unit tests run every time you save
# changes to a .swift file
#
# Prerequisites: fswatch, available vi `brew install fswatch`
# Test stuff now
swift test 2>&1 | xcpretty 2>/dev/null