Skip to content

Instantly share code, notes, and snippets.

View radude89's full-sized avatar
🏖️
Swifting

Radu Dan radude89

🏖️
Swifting
View GitHub Profile
@radude89
radude89 / optional-implementation.swift
Last active May 18, 2019 14:25
Swift's gems nil, void and Never - Optional in Swift 5
enum Optional<Wrapped>: ExpressibleByNilLiteral {
case none
case some(Wrapped)
}
@radude89
radude89 / example-of-nils.swift
Created May 18, 2019 14:26
Swift's gems nil, void and Never - Example of nil
let greeting: String? = nil // preferred
let greeting2: String? = .none // same thing
let greeting3: Optional<String> = .none // same thing
@radude89
radude89 / void-example-1.swift
Created May 18, 2019 14:27
Swift's gems nil, void and Never - void example 1
func doSomeStuff() {
// magic stuff
}
@radude89
radude89 / void-example-1b.swift
Created May 18, 2019 14:27
Swift's gems nil, void and Never - void 1b
func doSomeStuff() -> Void {
// magic stuff
}
@radude89
radude89 / void-example-2.swift
Created May 18, 2019 14:28
Swift's gems nil, void and Never - Example 2
func doSomeStuff() {} // preferred
func doSomeStuff2() -> Void {} // same thing
func doSomeStuff3() -> () {} // same thing
@radude89
radude89 / void-example-3.swift
Created May 18, 2019 14:29
Swift's gems nil, void and Never - Void Example 3
func doSomeStuff(completion: () -> Void) {
// magic stuff
}
@radude89
radude89 / void-example-4a.swift
Created May 18, 2019 14:31
Swift's gems nil, void and Never - Void Example 4a
func doSomeStuff(completion: (Void) -> ()) {
}
func doSomeStuff2(completion: () -> ()) {
}
@radude89
radude89 / void-example-4b.swift
Created May 18, 2019 14:32
Swift's gems nil, void and Never - Void Example 4b
func outside() {
doSomeStuff { result in
// some closure stuff
}
doSomeStuff2 {
// some closure stuff
}
}
@radude89
radude89 / never-example-1.swift
Created May 18, 2019 14:33
Swift's gems nil, void and Never - Never Example 1
func dumpAndCrush() -> Never {
// export output to log
// save
fatalError("App went dark! Check the logs why.")
}
// fatalError declaration
func fatalError(_ message: @autoclosure () -> String = String(),
file: StaticString = #file,
line: UInt = #line) -> Never
@radude89
radude89 / never-example-2.swift
Created May 18, 2019 14:34
Swift's gems nil, void and Never - Never Example 2
func doSomeStuff(with numbers: [Int]) {
guard numbers.count > 5 else {
preconditionFailure("Stuff happens when there are more than five numbers!")
}
// do some stuff
}
// preconditionFailure declaration
func preconditionFailure(_ message: @autoclosure () -> String = String(),