Skip to content

Instantly share code, notes, and snippets.

import Foundation
public func cast<T>(from any: Any?) -> T {
guard let type = any as? T else {
fatalError()
}
return type
@chrismsimpson
chrismsimpson / Environment.swift
Created October 5, 2019 02:47
Environment extensions for Swift
import Foundation
public enum Environment: String {
// A plist should exist for each of the environments
// below. For example `case production = "Prod"` will
// have a corresponding `Prod.plist`.
case production
case staging
@chrismsimpson
chrismsimpson / Decodable.swift
Last active October 5, 2019 02:32
Codable extensions for Swift
import Foundation
extension Decodable {
public static func from(data: Data) throws -> Self {
return try JSONDecoder()
.decode(self, from: data)
}
}
@chrismsimpson
chrismsimpson / Collection.swift
Last active October 5, 2019 02:13
Collection extensions for Swift
import Foundation
extension Collection {
public var nonEmpty: Self? {
guard !self.isEmpty else {
return nil
}
@chrismsimpson
chrismsimpson / Parallel.swift
Last active October 13, 2019 03:30
Parallel extensions for Swift
//
// Parallel.swift
//
import Dispatch
extension DispatchQueue {
public func parallel(_ blocks: Array<() -> Void>) {
@chrismsimpson
chrismsimpson / Regex.swift
Last active September 28, 2019 05:49
Regex Helpers for Swift
import Foundation
public struct Regex {
private let expression: NSRegularExpression
private let pattern: String
public init?(pattern _pattern: String) {