View OneOf.swift
public extension Equatable {
func isOne(of others: Self...) -> Bool {
return isOne(of: others)
}
func isOne<T: Collection>(of others: T) -> Bool where T.Element == Self {
return others.contains(self)
}
}
View UTType.swift
import Foundation
import MobileCoreServices
struct UTType: CustomStringConvertible {
let value: CFString
init?(mimeType: String) {
guard let UTI = UTTypeCreatePreferredIdentifierForTag(kUTTagClassMIMEType, mimeType as CFString, nil)?.takeUnretainedValue() else { return nil }
value = UTI
}
View Dictionary+EnumSubscript.swift
extension Dictionary {
subscript<T: RawRepresentable>(_ key: T) -> Value? where T.RawValue == Key {
return self[key.rawValue]
}
}
// Usage:
enum Type: Int {
case easy
}
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"]
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
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)
}
}