Last active
May 25, 2023 16:40
-
-
Save MohamedAymenHADDAD/34dad0ef7e97db1d9da11a15b43c6708 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
// | |
// DebugProperty.swift | |
// | |
// Created by Mohamed Haddad | |
// | |
@propertyWrapper | |
internal struct DebugProperty<Value> { | |
private var defaultValue: Value | |
private var readOnly: Bool | |
private var debugValue: Value | |
private let description: String | |
var wrappedValue: Value { | |
get { | |
#if DEBUG | |
print("### [DEBUG] ### \(description)") | |
return debugValue | |
#else | |
return defaultValue | |
#endif | |
} | |
set { | |
guard !readOnly else { | |
assertionFailure("### [DEBUG] ### Debug properties are READ ONLY !") | |
return | |
} | |
#if DEBUG | |
debugValue = newValue | |
#else | |
defaultValue = newValue | |
#endif | |
} | |
} | |
init(wrappedValue: Value, defaultValue: Value, readOnly: Bool, debugDescription: String = "DebugPropertyWrapper") { | |
self.debugValue = wrappedValue | |
self.description = debugDescription | |
self.defaultValue = defaultValue | |
self.readOnly = readOnly | |
} | |
} | |
#if DEBUG | |
@propertyWrapper | |
internal struct DebugOnlyProperty<Value> { | |
private var debugValue: Value | |
private let description: String | |
private var readOnly: Bool | |
var wrappedValue: Value { | |
get { | |
print("### [DEBUG] ### \(description)") | |
return debugValue | |
} | |
set { | |
guard !readOnly else { | |
assertionFailure("### [DEBUG] ### Debug properties are READ ONLY !") | |
return | |
} | |
debugValue = newValue | |
} | |
} | |
init(wrappedValue: Value, readOnly: Bool, debugDescription: String = "DebugPropertyWrapper") { | |
self.readOnly = readOnly | |
self.debugValue = wrappedValue | |
self.description = debugDescription | |
} | |
} | |
#endif | |
/// useCase | |
@DebugOnlyProperty(readOnly: false) | |
static var myCrazyString: String? = nil | |
@DebugProperty(defaultValue: true, readOnly: true) | |
static var myCrazyBool: Bool = { | |
return isEnabled | |
}() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment