Skip to content

Instantly share code, notes, and snippets.

@sparklone
Created July 12, 2020 15:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sparklone/6f64156ed6de87c1020198d93f4c45a3 to your computer and use it in GitHub Desktop.
Save sparklone/6f64156ed6de87c1020198d93f4c45a3 to your computer and use it in GitHub Desktop.
Helper to debug SwiftUI view lifecycle and redraw
import SwiftUI
public struct DebugView<MainView: View>: View {
private let view: MainView
private let logType: LogType
private enum LogType {
case onlyDescription(String)
case descriptionAndDumpView(String)
case dumpView
}
private var about: String {
switch logType {
case let .onlyDescription(description):
return "\(description)"
case let .descriptionAndDumpView(description):
return "\(description): \(view)"
case .dumpView:
return "\(view)"
}
}
public init(view: MainView, description: String?, dumpView: Bool = true) {
self.view = view
if let description = description {
if dumpView {
logType = .descriptionAndDumpView(description)
} else {
logType = .onlyDescription(description)
}
} else {
logType = .dumpView
}
print("init: \(about)")
}
public var body: some View {
print("body: \(about)")
return view
}
}
extension View {
public func debug() -> DebugView<Self> {
return DebugView(view: self, description: nil)
}
public func debug(_ description: String, dumpView: Bool = false) -> DebugView<Self> {
return DebugView(
view: self,
description: description,
dumpView: dumpView
)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment