Skip to content

Instantly share code, notes, and snippets.

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:
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.
import Foundation
private let log = Log(topic: .rateLimit)
/// Timeout based rate limit helper class.
final class RateLimit {
// MARK: - Private
private let timeout: TimeInterval
final class OnceToken {
private(set) lazy var perform: () -> Void = {
self.closure()
return {}
}()
private let closure: () -> Void
init(execute closure: @escaping () -> Void) {
@daehn
daehn / asset_extensions.stencil
Last active August 31, 2021 18:58
SwiftGen Stencil template to create UIColor and UIImage extensions for all asset catalogs.
/// 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 November 12, 2018 13:27
Lightweight logging class supporting different topics
import Foundation
final class Log: NSObject {
enum Topic: String {
case network, app
}
private enum Level: String {
case info = ""
@daehn
daehn / Reachability.swift
Created October 24, 2018 11:01
iOS Reachability Helper
import Foundation
import SystemConfiguration
extension Notification.Name {
public static let reachabilityChanged = Notification.Name(rawValue: "reachabilityChanged")
}
protocol ReachabilityObserver: class {
func reachabilityDidChange(_ reachability: Reachability)
}
import Foundation
extension Equatable {
/// Whether `self` is contained in a list of other values.
/// Variadic version, see the method below for the generic implementation.
///
/// - Parameter others: The values that `self` should be checked against.
/// - Returns: Whether or not `self` is one of the provided other values.
func isOne(of others: Self...) -> Bool {
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 October 5, 2017 09:38
Subscript a dictionary with an enum having a matching rawValue type without having to use .rawValue after the case
extension Dictionary {
subscript<T: RawRepresentable>(_ key: T) -> Value? where T.RawValue == Key {
return self[key.rawValue]
}
}
// Usage:
enum Type: Int {
case easy
}