Instantly share code, notes, and snippets.

Silvan Daehn daehn

View GitHub Profile
@daehn
daehn / asset_extensions.stencil
Last active Jan 30, 2019
SwiftGen Stencil template to create UIColor and UIImage extensions for all asset catalogs.
View asset_extensions.stencil
/// Attention: Changes made to this file will not have any effect and will be reverted
/// when building the project. Please adjust the Stencil template `asset_extensions.stencil` instead.
/// See https://github.com/SwiftGen/SwiftGen#bundled-templates-vs-custom-ones for more information.
import UIKit
// MARK: - Private Helper -
private final class BundleToken {}
private let bundle = Bundle(for: BundleToken.self)
@daehn
daehn / Log.swift
Last active Nov 12, 2018
Lightweight logging class supporting different topics
View Log.swift
import Foundation
final class Log: NSObject {
enum Topic: String {
case network, app
}
private enum Level: String {
case info = ""
@daehn
daehn / Reachability.swift
Created Oct 24, 2018
iOS Reachability Helper
View Reachability.swift
import Foundation
import SystemConfiguration
extension Notification.Name {
public static let reachabilityChanged = Notification.Name(rawValue: "reachabilityChanged")
}
protocol ReachabilityObserver: class {
func reachabilityDidChange(_ reachability: Reachability)
}
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
}
@daehn
daehn / Dictionary+EnumSubscript.swift
Created Oct 5, 2017
Subscript a dictionary with an enum having a matching rawValue type without having to use .rawValue after the case
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"]
@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
}