Created
April 6, 2022 21:28
-
-
Save erikmartens/b34a130d11b62400ab13a59a6c3dbd91 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// @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