Skip to content

Instantly share code, notes, and snippets.

@erikmartens
Created April 6, 2022 21:28
Show Gist options
  • Save erikmartens/b34a130d11b62400ab13a59a6c3dbd91 to your computer and use it in GitHub Desktop.
Save erikmartens/b34a130d11b62400ab13a59a6c3dbd91 to your computer and use it in GitHub Desktop.
//
// @Reloadable.swift
// NearbyWeather
//
// Created by Erik Maximilian Martens on 06.04.22.
// Copyright © 2022 Erik Maximilian Martens. All rights reserved.
// Based on: https://stackoverflow.com/questions/25072597/re-initialize-a-lazy-initialized-variable-in-swift
//
import Foundation
@propertyWrapper class Reloadable<T: AnyObject> {
private let initializer: (() -> T)
private var _wrappedValue: T?
var wrappedValue: T {
if _wrappedValue == nil {
_wrappedValue = initializer()
}
return _wrappedValue!
}
init(initializer: @escaping (() -> T)) {
self.initializer = initializer
}
func nuke() {
_wrappedValue = nil
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment