Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // CachedValue.swift
- //
- // Created by Chris Martin on 6/18/18.
- //
- import Foundation
- class CachedValue<Element> {
- typealias ElementCreator = () -> Element
- private let creator: ElementCreator
- private var cachedValue: Element?
- public var value: Element {
- if let value = cachedValue {
- return value
- }
- else {
- let value = creator()
- cachedValue = value
- return value
- }
- }
- public init(creator: @escaping ElementCreator) {
- self.creator = creator
- }
- public func invalidate() {
- cachedValue = nil
- }
- }
Add Comment
Please, Sign In to add comment