Skip to content

Instantly share code, notes, and snippets.

Avatar

Silvan Dähn daehn

View GitHub Profile
@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 XCTestCase+Await.swift
extension XCTestCase {
struct AwaitError: Error {}
/**
This function is useful for asynchronous testing and acts as a wrapper around test expectations.
Consider the following example:
```swift
// Let's asume we have an async function with the following signature:
View OneOf.swift
extension Equatable {
func oneOf(other: Self...) -> Bool {
return other.contains(self)
}
}
@daehn
daehn / asset_extensions.stencil
Last active May 15, 2020
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)
View XCTestCase+Fixture.swift
extension XCTestCase {
enum FixtureError: Error {
case resourceNotFound
case invalidFormat
}
/// Loads the json fixture with the given name from disk and returns a json object.
/// - Parameter fileName: The anme of the fixture on disk (without the type suffix).
/// - Throws: Any errors encountered during the process.
View RateLimit.swift
import Foundation
private let log = Log(topic: .rateLimit)
/// Timeout based rate limit helper class.
final class RateLimit {
// MARK: - Private
private let timeout: TimeInterval
View OnceToken.swift
final class OnceToken {
private(set) lazy var perform: () -> Void = {
self.closure()
return {}
}()
private let closure: () -> Void
init(execute closure: @escaping () -> Void) {
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 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 / Searchable.swift
Last active Apr 23, 2019
Swift protocols and protocol extensions to easily add and delete conforming instances to Spotlight. Blog post can be found here: http://silvandaehn.com/2015/09/25/Simplifying-CoreSpotlight/
View Searchable.swift
import UIKit
import CoreSpotlight
import MobileCoreServices
public typealias Completion = NSError? -> Void
// MARK: - Searchable Protocol
public protocol Searchable {
static var spotlightDomainIdentifier: String { get }