SwiftUI Tips:在 ViewBuilder 中方便的打印调试信息

我们在调试 SwiftUI View 的时候有时需要在打印一些内容。最常见的打印方式是使用print

swift 复制代码
struct ContentView: View {
    var body: some View {
        VStack {
            Image(systemName: "globe")
                .foregroundStyle(.tint)
            let _ = print("my log")
            Text("Hello, world!")
        }
        .padding()
    }
}

但是如果想要打印某个 View 节点的信息(类型、属性)就有点不方便了。

假设我有一个自定义的控件,MyText,有一个公开的属性 title

swift 复制代码
struct MyText: View {
    let title: String
    
    var body: some View {
        Text(title)
            .foregroundColor(.red)
    }
}

要如何调试打印这个 MyText 的title 呢?这个时候直接使用 print 就不好用了。硬来的话只能多复制一个 MyText 了:

swift 复制代码
struct PrintView: View {
    var body: some View {
        VStack {
            MyText(title: "我的内容")
            let _ = print("mylog:\(MyText(title: "我的内容").title)")
        }
        .padding()
    }
}

可以通过自定义一个泛型打印函数来解决这个问题:

swift 复制代码
func printThrough<T>(
    _ t: T, 
    _ message: String? = nil, 
    map: ((T) -> String)? = nil
) -> T {
    let description = if let map {
        map(t)
    } else {
        String(describing: t)
    }

    let message = if let message {
        "\(message): '\(description)'"
    } else {
        description
    }

    print(message)

    return t
}

printThrough 第一个参数接收任意内容(view)。第二个参数可空接收要打印的字符串前缀,如果没有传入默认打印第一个参数的类型信息。第三个参数是个闭包,在闭包中可以获得第一个参数的实例,用于打印自定义的实例信息。

有了这个函数以后我们前面的需求就可以优雅地实现了:

swift 复制代码
struct ContentView: View {
    var body: some View {
        VStack {
            printThrough(
                Text("Hello, world!")
            )
            printThrough(
                MyText(title: "我的内容"),
                "MyLog",
                map: { "内容是:\($0.title)"})
        }
        .padding()
    }
}

控制台就会输出这样的结果:


相关推荐
东坡肘子2 天前
OpenClaw 不错,但我好像没有那么需要 -- 肘子的 Swift 周报 #125
人工智能·swiftui·swift
东坡肘子9 天前
春晚、机器人、AI 与 LLM -- 肘子的 Swift 周报 #124
人工智能·swiftui·swift
君赏9 天前
第三十二章 接下来我们开始做`灭菌整板`页面
swiftui
君赏9 天前
第三十章 接下来我们写首页的功能,首先是我们的`托盘绑定箱号`。
swiftui
君赏9 天前
第三十一章 完善箱号列表
swiftui
君赏9 天前
第二十五章 完善登录逻辑
swiftui
君赏9 天前
第二十六章 Focused
swiftui
君赏9 天前
第 二十章 @Published sink
swiftui
君赏9 天前
第二十一章 @ViewBuilder默认实现|Toggle|我的页面封装
swiftui
君赏9 天前
第二十九章 修复首页 PopMenuView 显示问题
swiftui