Created
April 22, 2022 08:07
-
-
Save TerjeLon/f8e1e4487b8abb74dd221ed9faff1a54 to your computer and use it in GitHub Desktop.
Property wrapper for changing value when in SwiftUI Preview mode
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
import Foundation | |
@propertyWrapper public struct Preview<Value> { | |
let isPreview = ProcessInfo.processInfo.environment["XCODE_RUNNING_FOR_PREVIEWS"] == "1" | |
public var wrappedValue: Value { | |
get { | |
return isPreview ? previewValue : defaultValue | |
} | |
set { | |
if isPreview { | |
previewValue = newValue | |
} else { | |
defaultValue = newValue | |
} | |
} | |
} | |
private var defaultValue: Value | |
private var previewValue: Value | |
public init(wrappedValue defaultValue: Value, _ previewValue: Value) { | |
self.defaultValue = defaultValue | |
self.previewValue = previewValue | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment