ObjectUI/README.md

70 lines
1.6 KiB
Markdown
Raw Normal View History

2021-05-25 10:38:48 +08:00
# ObjectUI
2021-05-25 10:48:14 +08:00
*Create SwiftUI Views with any data*
2021-06-23 08:56:51 +08:00
## Usage
```swift
import ObjectUI
```
### Basic Example
```swift
struct ContentView: View {
var body: some View {
ObjectView { object in
VStack {
if let value = object.value(as: String.self) {
Text(value)
.font(.largeTitle)
} else {
Text("Waiting...")
}
Button("Wave") {
2021-06-23 09:00:18 +08:00
DispatchQueue.main.async {
object.set(value: "👋")
}
2021-06-23 08:56:51 +08:00
}
}
}
}
}
```
2021-06-23 09:13:00 +08:00
### JSON Example
```swift
struct ContentView: View {
let json = """
{
"userId": 1,
"id": 2,
"title": "quis ut nam facilis et officia qui",
"completed": false
}
"""
.data(using: .utf8)
var body: some View {
ObjectView(data: json) { object in
VStack {
object.userId.value(as: Int.self).map { userId in
Text("\(userId)")
}
object.id.value(as: Int.self).map { id in
Text("\(id)")
}
object.title.value(as: String.self).map { title in
Text(title)
}
object.completed.value(as: Bool.self).map { completed in
Text(completed.description)
}
}
}
}
}
```