Skip to content

Instantly share code, notes, and snippets.

Avatar

Silvan Dähn daehn

View GitHub Profile
View DispatchQueue+Helper.swift
extension DispatchQueue {
/// Helper method to use when reading from a resource
/// that is being isolated by this queue.
///
/// Using this method retriving a value like this:
/// ```
/// var result: T?
/// queue.sync {
/// result = resource["key"]
@daehn
daehn / Throttle.swift
Created Jun 23, 2017
No more `performSelector:afterDelay` and `NSObject.cancelPreviousPerformRequests` in Swift.
View Throttle.swift
class Throttle {
let delay: TimeInterval
private var workItem: DispatchWorkItem?
private let queue: DispatchQueue
init(delay: TimeInterval, queue: DispatchQueue = .main) {
self.delay = delay
self.queue = queue
}
View InterpolatingAnimator.swift
import UIKit
import PlaygroundSupport
PlaygroundPage.current.needsIndefiniteExecution = true
extension Comparable {
/// Clamp the value between `low` and `high`
func clamp(_ low: Self, _ high: Self) -> Self {
View CompareDumps.swift
struct Car {
let made: String
}
func compareDumps(lhs: Any, rhs: Any) -> Bool {
var (lhsDump, rhsDump) = (String(), String())
dump(lhs, to: &lhsDump)
dump(rhs, to: &rhsDump)
return lhsDump == rhsDump
}
View NSPredicateOperatorBug.swift
import Foundation
// Defining this breaks the behaviour of the existing '||' operator in some cases
public func ||(lhs: NSPredicate, rhs: NSPredicate) -> NSPredicate {
return NSCompoundPredicate(orPredicateWithSubpredicates: [lhs, rhs])
}
func foo() {
let cards = [String: [String]]()
let startingContactIndex: UInt = 3
@daehn
daehn / UIView+Debugging.swift
Last active Oct 14, 2016
UIView debugging helper
View UIView+Debugging.swift
public extension UIView {
static var debugColors: [UIColor] {
return [
.red,
.green,
.blue,
.cyan,
.yellow,
.magenta,
View OneOf.swift
extension Equatable {
func oneOf(other: Self...) -> Bool {
return other.contains(self)
}
}
View KeyboardFrameObserver.swift
import UIKit
/// Object observing keyboard changes and passing a `KeyboardChangeInfo` to notify about changes.
final class KeyboardObserver: NSObject {
typealias KeyboardChangeClosure = (KeyboardChangeInfo) -> Void
let changeClosure: KeyboardChangeClosure
// MARK: - Private
@daehn
daehn / TypedKVOClosure.swift
Last active Jul 26, 2016
Helper class to provide a typed, closure based KVO interface in Swift
View TypedKVOClosure.swift
import Foundation
struct Change<T>: CustomDebugStringConvertible {
let oldValue, newValue: T?
var debugDescription: String {
let prettyString: T? -> String = { return $0 != nil ? "\($0!)" : ".None" }
return "ChangeType:\n\tOld value: \(prettyString(oldValue))\n\tNew value: \(prettyString(newValue))"
}
}
View CountedSet.swift
public struct CountedSet<Element : Hashable> : Hashable, CollectionType, ArrayLiteralConvertible {
public typealias Index = SetIndex<Element>
public typealias Generator = SetGenerator<Element>
private var backingSet = Set<Element>()
private var countMapping = [Int: UInt]()
private var debugCountMapping: [Element : UInt] {
var result = [Element : UInt]()
backingSet.forEach { element in