[英]Load json from @propertyWrapper in SwiftUI ContentView
I've been using a json decoding utility that works great.我一直在使用一个很好用的 json 解码实用程序。 I'm wondering if that utility can be abstracted into a propertyWrapper that accepts the json file name as a string.
我想知道是否可以将该实用程序抽象为接受 json 文件名作为字符串的 propertyWrapper。
The call site in the ContentView looks like this: ContentView 中的调用站点如下所示:
struct ContentView: View {
@DataLoader("tracks.json") var tracks: [Tracks]
...
My rough sketch of the property wrapper looks like this:我对属性包装器的粗略草图如下所示:
@propertyWrapper
struct DataLoader<T: Decodable>: DynamicProperty {
private let fileName: String
var wrappedValue: T {
get {
return Bundle.main.decode(T.self, from: fileName)
}
set {
//not sure i need to set anything since i just want to get the array
}
}
init(_ fileName: String) {
self.fileName = fileName
wrappedValue = Bundle.main.decode(T.self, from: fileName)
}
}
Currently the body of the the ContentView shows this error:当前 ContentView 的主体显示此错误:
Failed to produce diagnostic for expression;
未能产生表达诊断; please file a bug report
请提交错误报告
I like the idea of removing some boilerplate code, but I think I'm missing something fundamental here.我喜欢删除一些样板代码的想法,但我认为我在这里遗漏了一些基本的东西。
In SwiftUI views are refreshed very often.在 SwiftUI 中,视图经常刷新。 When a view is refreshed then the
@propertyWrapper
will be initialised again - this might or might not be desirable.当刷新视图时,
@propertyWrapper
将再次初始化 - 这可能是也可能不是可取的。 But it's worth noting.但值得注意的是。
Here is a simple demo showing how to create a property wrapper for loading JSON files.这是一个简单的演示,展示了如何创建用于加载 JSON 文件的属性包装器。 For simplicity I used
try?
为简单起见,我用
try?
and fatalError
but in the real code you'll probably want to add a proper error handling.和
fatalError
但在实际代码中,您可能希望添加适当的错误处理。
@propertyWrapper
struct DataLoader<T> where T: Decodable {
private let fileName: String
var wrappedValue: T {
guard let result = loadJson(fileName: fileName) else {
fatalError("Cannot load json data \(fileName)")
}
return result
}
init(_ fileName: String) {
self.fileName = fileName
}
func loadJson(fileName: String) -> T? {
guard let url = Bundle.main.url(forResource: fileName, withExtension: "json"),
let data = try? Data(contentsOf: url),
let result = try? JSONDecoder().decode(T.self, from: data)
else {
return nil
}
return result
}
}
Then, assuming you have a sample JSON file called items.json
:然后,假设您有一个名为
items.json
的示例 JSON 文件:
[
{
"name": "Test1",
"count": 32
},
{
"name": "Test2",
"count": 15
}
]
with a corresponding struct:具有相应的结构:
struct Item: Codable {
let name: String
let count: Int
}
you can load your JSON file in your view:您可以在视图中加载 JSON 文件:
struct ContentView: View {
@DataLoader("items") private var items: [Item]
var body: some View {
Text(items[0].name)
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.