我想为订单状态创建一个像这样的enum
struct Order: Identifiable, Hashable {
var id: Int {
return elementID
}
var elementID: Int
var orderNumber: String
enum Status: String {
case complete = "Completed"
case transit = "In Transit"
case prepare = "Prepairing"
}
var date: String
func toString(forStatus status: Status) -> String {
return status.rawValue
}
}
然后我想在OrderList中创建一个订单,如下所示
struct OrderList {
static let listData: [Order] = [
Order(elementID: 0, orderNumber: "123123123123", date: "15.08.2020", status: .transit)
]
}
或者像这样
struct OrderList {
static let listData: [Order] = [
Order(elementID: 0, orderNumber: "123123123123", date: "15.08.2020").toString(forStatus: .complete))
]
}
但是我可以到达OrderList中的状态。如何在Order to Status中设置值?
您忘记将status属性添加到Order
。
var status: Status
如果你打算嵌套Status
(你应该这样做!),那么你也应该嵌套List
。
extension Order {
struct List {
static let data = [
Order(elementID: 0, orderNumber: "123123123123", date: "15.08.2020", status: .transit)
]
}
}