Skip to content

Instantly share code, notes, and snippets.

@jrBordet
Created May 20, 2020 13:15
Show Gist options
  • Save jrBordet/55c884168d11d42978c164f406c5c92b to your computer and use it in GitHub Desktop.
Save jrBordet/55c884168d11d42978c164f406c5c92b to your computer and use it in GitHub Desktop.
import UIKit
extension Date: ExpressibleByStringLiteral {
public init(stringLiteral value: String) {
let formatter = DateFormatter()
formatter.dateFormat = "dd-MM-yyyy"
formatter.timeZone = TimeZone(secondsFromGMT: 0)
self = formatter.date(from: value)!
}
}
let date: Date = "09-12-2020"
print(date) // Prints: 2020-12-09 00:00:00 +0000
final class Cache<T> {
private var store: [String: T]
init(objects: [String: T]) {
self.store = objects
}
func cache(_ object: T, forKey key: String) {
store[key] = object
}
func object(for key: String) -> T? {
return store[key]
}
}
extension Cache: ExpressibleByDictionaryLiteral {
convenience init(dictionaryLiteral elements: (String, T)...) {
self.init(objects: [String: T](uniqueKeysWithValues: elements))
}
}
let cache: Cache<URL> = ["Swift": URL(string: "https://www.some.com")!]
print(cache.object(for: "Swift")!)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment