Skip to content

Instantly share code, notes, and snippets.

@TerjeLon
Created April 22, 2022 08:07
Show Gist options
  • Save TerjeLon/f8e1e4487b8abb74dd221ed9faff1a54 to your computer and use it in GitHub Desktop.
Save TerjeLon/f8e1e4487b8abb74dd221ed9faff1a54 to your computer and use it in GitHub Desktop.
Property wrapper for changing value when in SwiftUI Preview mode
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