Lor*_*Ang 2 xcode swift swiftui
我正在尝试重新创建 SwiftUI 演示,不同之处在于我想使用我自己的对象Item。
物品:
class Item {
var company: String = ""
var item_class: String = ""
var name: String = ""
var stock: Int = 0
var average_cost: Decimal = 0.00
var otc_price: Decimal = 0.00
var dealer_price: Decimal = 0.00
var ctc_price: Decimal = 0.00
class var _API_LIST_EP: String {return "api/inventory/items/"}
// Init and Funcs
// JToken is an extended typealias for [String : Any] that makes parsing easier
required init(_ jt: JToken) {
company = jt.string(forKey: "company")
item_class = jt.string(forKey: "item_class")
name = jt.string(forKey: "name")
stock = jt.int(forKey: "stock")
average_cost = jt.decimal(forKey: "average_cost")
otc_price = jt.decimal(forKey: "otc_price")
dealer_price = jt.decimal(forKey: "dealer_price")
ctc_price = jt.decimal(forKey: "ctc_price")
}
}
Run Code Online (Sandbox Code Playgroud)
在一个类似的问题中,有人指出该问题源于对象的一个变量在不明确的状态下初始化,但是在填充我所有对象的变量后,它仍然出现
有问题的代码:
struct ContentView : View {
var itemList: [Item] = []
var body: some View {
List(itemList) { item in
Image(systemName: "photo")
VStack(alignment: .leading) {
Text(item.name)
Text(item.company)
.font(.subheadline)
.color(.gray)
}
}
}
}
Run Code Online (Sandbox Code Playgroud)
错误截图:
错误消息具有误导性。
List(itemList) { item in ... }
Run Code Online (Sandbox Code Playgroud)
要求元素类型itemList符合Identifiable协议。对于类(如您的情况),声明协议一致性就足够了
class Item: Identifiable {
// ...
}
Run Code Online (Sandbox Code Playgroud)
因为有一个默认实现(基于对象标识符)。
| 归档时间: |
|
| 查看次数: |
2294 次 |
| 最近记录: |