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.
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") )
Print Binary String of Integers
A generic function to print the “binary string” of any FixedWidthInteger
.
Example:
let smallNumber: UInt16 = 7
print(asBinary: smallNumber) //output: 0b0000000000000111
Future Improvements
- Comments.
- 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.