PropertyInspector 是一个 SwiftUI 组件,它提供了一种强大而灵活的方式来动态地检查和交互属性。它专为希望创建复杂的调试工具、增强其应用程序的交互性,或者仅仅需要详细查看其数据结构的开发人员而设计。所有这些都只需最少的代码和一个简单的 API。
通过将 swiftui-property-inspector
包含在您的 Package.swift
文件中,将其添加到您的项目中
dependencies: [
.package(url: "https://github.com/ipedro/swiftui-property-inspector", .upToNextMajor(from: "1.0.0"))
]
然后,在您的 SwiftUI 视图中导入 swiftui-property-inspector
以开始使用它。
swiftui-property-inspector
的完整文档可以在这里找到。
这是一个在 SwiftUI 视图中使用 PropertyInspector
的简单示例
import PropertyInspector
import SwiftUI
var body: some View {
PropertyInspector(listStyle: .plain) {
VStack(content: {
InspectableText(content: "Placeholder Text")
InspectableButton(style: .bordered)
})
.propertyInspectorRowLabel(for: Int.self, label: { data in
Text("Tap count: \(data)")
})
.propertyInspectorRowIcon(for: Int.self, icon: { data in
Image(systemName: "\(data).circle.fill")
})
.propertyInspectorRowIcon(for: String.self, icon: { _ in
Image(systemName: "text.quote")
})
.propertyInspectorRowIcon(for: (any PrimitiveButtonStyle).self, icon: { _ in
Image(systemName: "button.vertical.right.press.fill")
})
}
}
struct InspectableText<S: StringProtocol>: View {
var content: S
var body: some View {
Text(content).inspectProperty(content)
}
}
struct InspectableButton<S: PrimitiveButtonStyle>: View {
var style: S
@State private var tapCount = 0
var body: some View {
Button("Tap Me") {
tapCount += 1
}
// inspecting multiple values with a single function call links their highlight behavior.
.inspectProperty(style, tapCount)
.buttonStyle(style)
}
}
禁用属性检查
var body: some View {
MyView().propertyInspectorHidden()
}
我们欢迎贡献!如果您想贡献代码,请 fork 该仓库并使用一个 feature 分支。热烈欢迎 pull 请求。
swiftui-property-inspector
包是在 MIT 许可下发布的。有关详细信息,请参阅 LICENSE。