Skip to content

Instantly share code, notes, and snippets.

@qmchenry
Last active February 22, 2022 16:57
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 qmchenry/81e0f25dbd128456b469502ba9f7c41b to your computer and use it in GitHub Desktop.
Save qmchenry/81e0f25dbd128456b469502ba9f7c41b to your computer and use it in GitHub Desktop.
dump() example
// DumpExample
// This SwiftUI View explores use of the dump() function for manipulating the debugging display
// of large, structured data.
//
// Created by Quinn McHenry on 2/17/22.
import SwiftUI
struct ContentView: View {
@State private var maxDepth = 0
@State private var maxItems = 0
private let maxMaxDepth = 6 // when maxDepth == this, use .max instead
private let maxMaxItems = 11 // when maxItems == this, use .max instead
private let sample = Alpha.sample()
var body: some View {
VStack {
Text("dump()")
.font(.title)
.padding()
Stepper("maxDepth: \(maxDepth == maxMaxDepth ? ".max" : "\(maxDepth)")",
value: $maxDepth, in: 0...maxMaxDepth, onEditingChanged: showDump)
Stepper("maxItems: \(maxItems == maxMaxItems ? ".max" : "\(maxItems)")",
value: $maxItems, in: 0...maxMaxItems, onEditingChanged: showDump)
}
.padding(50)
}
func showDump(_ changed: Bool) {
print("maxDepth = \(maxDepth) maxItems = \(maxItems)")
print("print() ---")
print(sample)
print("dump() ---")
dump(sample, //name: "Name here!", indent: 10,
maxDepth: maxDepth == maxMaxDepth ? .max : maxDepth,
maxItems: maxItems == maxMaxItems ? .max : maxItems)
/// Simple usage `dump(sample)` defaults to `dump(sample, maxDepth: .max, maxItems: .max)`
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
/// Fake data for testing
struct Alpha {
let s: String
let betas: [Beta]
static func sample() -> Self {
Alpha(s: "the alpha", betas: [
Beta(s: "beta one", deltas: [.sample(1), .sample(10), .sample(100)]),
Beta(s: "beta two", deltas: [.sample(2), .sample(20), .sample(200)]),
Beta(s: "beta three", deltas: [.sample(3), .sample(30), .sample(300)]),
Beta(s: "beta four", deltas: [.sample(4), .sample(20), .sample(400)]),
])
}
}
struct Beta {
let s: String
let deltas: [Delta]
}
struct Delta {
let i: Int
let s: String
static func sample(_ i: Int) -> Self {
Delta(i: i, s: "delta \(i)")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment