Skip to content

Instantly share code, notes, and snippets.

@nanoxd
nanoxd / main.swift
Created February 7, 2019 23:56
[Assert Main Queue] Assert you're on main queue. Note that main thread and main queue are not always the same thing http://blog.benjamin-encz.de/post/main-queue-vs-main-thread/
dispatchPrecondition(
condition: DispatchPredicate.onQueue(DispatchQueue.main)
)
@nanoxd
nanoxd / OptionalType+Sugar.swift
Last active December 30, 2018 11:13
[Optional+Sugar] Adds nice ways to interact with optionals #swift
/// Type erased Optional protocol
protocol OptionalType {
associatedtype WrapType
var isSome: Bool { get }
var isNone: Bool { get }
var unsafelyUnwrapped: WrapType { get }
}
@nanoxd
nanoxd / Either.swift
Created December 22, 2018 03:16
[Either] A type representing an alternative of one of two types.
/// A type representing an alternative of one of two types.
///
/// By convention, and where applicable, `Left` is used to indicate failure, while `Right` is used to indicate success. (Mnemonic: “right” is a synonym for “correct.”)
///
/// Otherwise, it is implied that `Left` and `Right` are effectively unordered alternatives of equal standing.
public enum Either<Left, Right> {
case left(Left)
case right(Right)
/// Returns the value of `Left` instances, or `nil` for `Right` instances.
@nanoxd
nanoxd / ObservableType+.swift
Last active December 16, 2018 02:36
[ObservableType+] Adds additional sugar to `ObservableType`
import Foundation
import RxSwift
import RxCocoa
extension ObservableType {
/// Unwraps an Optional Element if it's present
///
/// - Returns: The safely unwrapped item
func unwrap<T>() -> Observable<T> where E == Optional<T> {
return filter { $0 != nil }.map { $0! }
@nanoxd
nanoxd / HelloWorldViewModel.swift
Created December 16, 2018 02:34
[ViewModelType] Provide a clear contract between inputs/outputs desired in a view model
final class HelloWorldViewModel: ViewModelType {
let input: Input
let output: Output
struct Input {
let name: Anyobserver<String>
}
struct Output {
let greeting: Driver<String>
@nanoxd
nanoxd / KeyPath+Closures.swift
Last active December 10, 2018 12:20
Key Path for Closures
prefix operator ^
prefix func ^<Root, Value>(keyPath: KeyPath<Root, Value>) -> (Root) -> Value {
return { $0[keyPath: keyPath] }
}
@nanoxd
nanoxd / PermutationIterator.swift
Created December 10, 2018 00:49
[PermutationIterator] Use of Heap's Algorithm to create permutations
public struct PermutationIterator<T>: IteratorProtocol {
private var hasReturnedInitial = false
private var a: [T]
private var c: [Int]
private let n: Int
private var i = 0
public init<C: Collection>(_ values: C) where C.Element == T {
a = Array(values)
n = a.count
@nanoxd
nanoxd / lint
Created November 11, 2018 14:27
[bin/lint] Binary to run swiftlint, meant to be placed at the root of the project #sh
#!/bin/sh
#
# Lint any changed files using swiftlint.
#
# This works both independently and as a run phase script.
# Path to installed version of Swift lint.
readonly SWIFT_LINT_PATH=$(which swiftlint)
if [ -z "$SWIFT_LINT_PATH" ]; then
@nanoxd
nanoxd / FixBTSound.applescript
Last active November 11, 2018 14:26 — forked from mluisbrown/FixBTSound.applescript
[FixBTSound] AppleScript to set macOS audio input device to "Internal Microphone"
-- Sets your audio input source to "Internal Microphone"
-- Frequently needed if you use bluetooth headpohones and
-- run the Xcode iOS simulator, which will often set your
-- headphones to be the input device, resulting in a drastic
-- decrease in sound quality, and making it mono
tell application "System Preferences" to activate
tell application "System Preferences"
reveal anchor "input" of pane id "com.apple.preference.sound"
end tell
@nanoxd
nanoxd / UIWindow+TopLevel.swift
Created October 31, 2018 00:20
[UIWindow+TopLevel] Add a new window that rests above the key window #swift #ios
extension UIWindow {
final class StatusBarPreferringViewController: UIViewController {
// MARK: - Inputs
private let statusBarStyle: UIStatusBarStyle
// MARK: - Initialization
init(statusBarStyle: UIStatusBarStyle)
self.statusBarStyle = statusBarStyle