Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/rjstelling/Swift-Gists

A list of Gists containing simple but useful Swift code written by Richard Stelling - @rjstelling
https://github.com/rjstelling/Swift-Gists

swift

Last synced: about 1 month ago
JSON representation

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

Awesome Lists containing this project

README

        

# 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](https://gist.github.com/rjstelling/dfa3e6ac491806a63e359259a9a06dc3)

A `Version` type that supports [Semantic Versioning](https://semver.org) with labels.

```swift
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](https://gist.github.com/rjstelling/0e3298a42c2a2bca2f75f193578521c3)

A `URL` extension to make working with [UTIs](https://developer.apple.com/library/archive/documentation/FileManagement/Conceptual/understanding_utis/understand_utis_intro/understand_utis_intro.html#//apple_ref/doc/uid/TP40001319-CH201-SW1) in Swift less painful.

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

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

### [RegularExpressionValidation Property Wrapper](https://gist.github.com/rjstelling/649ff0e848af4e03f461386f4548b72d)

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:

```swift
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](https://github.com/rjstelling/StandardStreams.swift)

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

```swift
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](https://gist.github.com/rjstelling/0216a4f9bfaec87dec44d30ac65399b1)

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

```swift
// 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](https://gist.github.com/rjstelling/fc695f5c37beeefd2a810179b723b29f)

Data Extension for Fowler–Noll–Vo hash function.

```swift
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](https://gist.github.com/rjstelling/70c4d0ad6934df99246c0f56e3494f38)

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

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

### [Print Binary String of Integers](https://gist.github.com/rjstelling/3be83e4b5dbdb8b6278e324780938400)

A generic function to print the "binary string" of any `FixedWidthInteger`.

Example:

```swift
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.