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

Fixing Linker Errors in Xcode (___llvm_profile_runtime Missing Symbol)

This tutorial explains how to resolve the linker error ___llvm_profile_runtime missing symbol in Xcode, typically occurring when code coverage settings (CLANG_COVERAGE_MAPPING) are inadvertently enabled. The issue is common when using Swift Packages like PocketSVG. It guides you through verifying the problematic setting via command-line comparison of build settings, then demonstrates the solution—deleting the auto-generated test plan in Xcode’s test plan manager. After performing this simple step and cleaning your build, the linker error should be fixed.

How to Create and Use Multiple Apple IDs for iOS Simulator Testing

Creating multiple Apple IDs for testing your app in the iOS Simulator can help isolate user experiences and troubleshoot issues effectively. Follow this easy guide to set up and successfully use multiple Apple IDs.

Creating a UIBarButtonItem That Ignores Smart Invert

This tutorial shows how to create a custom UIBarButtonItem that ignores the Smart Invert accessibility feature (using accessibilityIgnoresInvertColors). It also covers why using a UIImageView as the custom view will not trigger an action, and offers a helpful example of an accessibility hint.

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.

Example:

struct Example {
    
    /* This will be set to nil unless the string assigned to it matches the regular expression 
    "^(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*|\"(?:[\\x01-\\x08\\x0b\\x0c\\x0e-\\x1f\\x21\\x23-\\x5b\\x5d-\\x7f]|\\[\\x01-\\x09\\x0b\\x0c\\x0e-\\x7f])*\")@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\\[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[a-z0-9-]*[a-z0-9]:(?:[\\x01-\\x08\\x0b\\x0c\\x0e-\\x1f\\x21-\\x5a\\x53-\\x7f]|\\[\\x01-\\x09\\x0b\\x0c\\x0e-\\x7f])+)\\])$"
    */
    @EmailValidation
    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
    @RegularExpressionValidation("^[£$][0-9,]*$")
    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.

Example:

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

Future Improvements

  1. Comments.
  2. Examples.

Pull Requests

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

Licence

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