Skip to content

Instantly share code, notes, and snippets.

View dnedrow's full-sized avatar

David Nedrow dnedrow

View GitHub Profile
@dnedrow
dnedrow / args.swift
Created April 7, 2019 00:55
Swift command line arguments
let argc = CommandLine.argc
let argv = CommandLine.arguments
// If the arguments have to be passed to UIApplicationMain, use the following to get the arguments.
let argv = UnsafeMutableRawPointer(CommandLine.unsafeArgv).bindMemory(to: UnsafeMutablePointer<Int8>.self, capacity: Int(CommandLine.argc))
@dnedrow
dnedrow / Defaults+ArrayOfDictionaries.swift
Created March 29, 2019 20:10
Support Array of Dictionary in UserDefaults
// Posted by basememara in https://github.com/radex/SwiftyUserDefaults/pull/68
public extension NSUserDefaults {
public subscript(key: DefaultsKey<[[String: AnyObject]]>) -> [[String: AnyObject]] {
get {
return arrayForKey(key._key) as? [[String: AnyObject]] ?? [[:]]
}
set { set(key, newValue) }
}
// David Nedrow
// See bottom for licence.
import UIKit
/// This protocol defines functions for determining contrasting colors and other
/// color related values.
public protocol ContrastColorProtocol {
/// Calculates the luminance of the given color.
///
@dnedrow
dnedrow / IntegerOrdinalFormatter.swift
Created March 1, 2019 03:48
Get the ordinal (e.g. 23rd for 23) representation of an integer
// This implementation from:
// https://stackoverflow.com/questions/3312935/nsnumberformatter-and-th-st-nd-rd-ordinal-number-endings
// Original author:
// https://stackoverflow.com/users/3624478/magoo
private var ordinalFormatter: NumberFormatter = {
let formatter = NumberFormatter()
formatter.numberStyle = .ordinal
return formatter
}()
@dnedrow
dnedrow / git.migrate
Created February 26, 2019 17:10 — forked from niksumeiko/git.migrate
Moving git repository and all its branches, tags to a new remote repository keeping commits history
#!/bin/bash
# Sometimes you need to move your existing git repository
# to a new remote repository (/new remote origin).
# Here are a simple and quick steps that does exactly this.
#
# Let's assume we call "old repo" the repository you wish
# to move, and "new repo" the one you wish to move to.
#
### Step 1. Make sure you have a local copy of all "old repo"
### branches and tags.
@dnedrow
dnedrow / createbranch
Last active February 13, 2019 13:59
Script to create a new git branch from a given repo/branch
#!/bin/zsh
# If necessary, adjust the above to point to your ZSH executable.
# Script to create a new git branch from a given repo/branch
# Written by David E Nedrow
# Last updated 2019-02-13 00:42 Eastern
# Check for the most recent version at:
# https://gist.github.com/dnedrow/4467592b0d8ecd7260451e7b01bf4f12
local base="upstream/develop"
@dnedrow
dnedrow / pag.sh
Last active December 26, 2018 19:54
Simple bash function that uses `ag` (Silver Surfer) to search for a process with the given name, while excluding `ag` itself.
#!/bin/bash
# Simple function that uses ag (Silver Surfer) to grep for a process,
# excluding ag itself. See the use of [] in AG_ARG.
function pag() {
if [ -x "$(which 'ag')" ]; then
typeset PROC_FIRST="${1[1]}"
typeset PROC_REST="${1:1}"
typeset AG_ARG="[${PROC_FIRST}]${PROC_REST}"
ps ax | ag "$AG_ARG"
@dnedrow
dnedrow / RandomNumbers.swift
Created October 2, 2018 21:17 — forked from jstn/RandomNumbers.swift
generate random numbers for 64-bit types while mitigating modulo bias
/*
`arc4random_uniform` is very useful but limited to `UInt32`.
This defines a generic version of `arc4random` for any type
expressible by an integer literal, and extends some numeric
types with a `random` method that mitigates for modulo bias
in the same manner as `arc4random`.
`lower` is inclusive and `upper` is exclusive, thus:
//
// UIViewAdditions.swift
//
// Created by Daniel Tartaglia on 04/15/15.
// Copyright © 2016. MIT License.
//
import UIKit

Two Level Type Erasing in Swift 3

Recently I converted a project of mine to Swift 3 (https://github.com/dtartaglia/XStreamSwift) and I had to deal with a problem that I couldn't find an answer for. This article is about the problem and the solution I discovered.

There are lots of articles on the web about type erasing in Swift, but all the ones I found only dealt with a single level. I will recap the concept quickly:

protocol Listener
{

associatedtype ListenerValue