Swift Gists by Richard Stelling

A list of Gists containing simple but useful Swift code written by Richard Stelling - @rjstelling

View on GitHub

Swift Gists

This is a continiously expanding list of GitHub Gists (and repos) containing simple but useful Swift code. Nothing here realy warrents a Framework or Library but can be copied and pasted.

Gists & Repositories

Semantic Versioning

A Version type that supports Semantic Versioning with labels.

let version1: Version = "1.0.0"
let version2: Version = "2.0.0"

version1 == version2 // false
version2 > version1 // true

let version1point1 = "1.1"

version1.isCompatible(version1point1) // true
version2.isCompatible(version1point1) // false

let version2Prerelease = "2.0-beta"

version2Prerelease >= version2 //false

Swifty Uniform Type Identifier Extensions

A URL extension to make working with UTIs in Swift less painful.

let documentUrl = URL(fileURLWithPath: "/Volumes/MyDisk/Projects/Project1/Report.pages"
let uti = try doumentUrl.uniformTypeIdentifier()

print("\(uti)") // Pages Document

RegularExpressionValidation Property Wrapper

A @propertyWrapper that validates a string using a regular expression. If the string matches then it is assigned to the property if it does not then the property is net to nil. Also included is an example of validating email addresses.


struct Example {
    /* This will be set to nil unless the string assigned to it matches the regular expression 
    var email: String?
    // The string must be in the format $1,000,000.00 or £1,000,000.00
    // Missing $ or £ will fail, `.` and `,` are optional
    var currency: String?    

Standard Streams

A protocol based approach to writing to Standard Out and Standard Error.

let console = StandardOut()
let error = StandardErr()

console.print("Hello World! - This will print to stdout and can be piped or redirect to a file.")

error.print("Errors are by standard printed to the console even if your redirect, useful!")

Base Functions

A set of extensions on String and FixedWidthInteger to encode and decode unsigned integers with custom character sets.

// Encode integer to a string
let encodedValue = try 1973.encode() // "PZ"

// Decode the string back to an integer
let decodedValue: UInt = try encodedValue.decode() // 1973

// Using a custom string
let rosetta = "🤖❤️☠️🤓🦁🍆🧀😎🙄🤢🧠🧶🙈🙉🙊🔥🥕⚽️💊🎶🧶👏🔡"
let emojiEncoded = try 1024.encode(rosetta.count, using: rosetta) // "❤️👏🙈"

let emojiDecoded: UInt = try "❤️👏🙈".decode(UInt(rosetta.count), using: rosetta) // 1024

Fowler–Noll–Vo Hash

Data Extension for Fowler–Noll–Vo hash function.

let data = "Hello World!".data(using: .utf8)!
let fnvValue = data.fnv32HashString() // "12a9a41c"

// Adding a single byte has a large affect on the hash
let fnvValue2 = (data + Data(repeating: 255, count: 1)).fnv32HashString() // "7d0d58eb"

Two Label UIButton

A simple Auto Layout solution for a UIButton with 2 labels.

let tlButton = TwoLabelButton()
tlButton.setTitles( ("Title:", "value") )

A generic function to print the “binary string” of any FixedWidthInteger.


let smallNumber: UInt16 = 7
print(asBinary: smallNumber) //output: 0b0000000000000111

Future Improvements

  2. Examples.

Pull Requests

If you feel like you want to, thats cool! However, I’m not promising to be speedy or meritocratic.


All code, including this file are covered by the MIT Licence.