我有以下结构:
struct PalletScan: Codable {
var deliveryId: String?
var userId: String?
var timestamp: String?
var tempPalletNr: String?
var tempLocation: String?
var tempPalletType: String?
var pallets: [MovementScan]?
//coding keys requried for translation API -> struct -> CoreData and CoreData -> struct -> API
enum CodingKeys: String, CodingKey {
case deliveryId = "TOID"
case userId = "UserId"
case timestamp = "TimeStamp"
}
mutating func appendMovementScan() {
var movementScan = MovementScan()
movementScan.locationId = self.tempLocation
movementScan.palletId = self.tempPalletNr
movementScan.palletType = self.tempPalletType
movementScan.timestamp = String(Date().timeIntervalSince1970)
print(movementScan)
self.pallets?.append(movementScan)
}
}
但是self.pallets?.append(movementScan)
没有向托盘数组添加任何内容。我错过了什么?它必须是微不足道的,但不能发现错误。
答案 0 :(得分:1)
只需更改var pallets: [MovementScan]?
以强>
var pallets: [MovementScan] = [MovementScan]()
作为@Carcigenicate你调用附加值nil值
答案 1 :(得分:0)
var pallet未初始化并且是可选的,所以当你使用附加运动扫描时? ,它不会被执行。
要解决此问题,您必须先了解如何在附加托盘数组之前对其进行初始化。 一种方法可以简单地用空数组初始化:
var pallets = [MovementScan]()