Reputation: 545
I have following struct:
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)
}
}
however self.pallets?.append(movementScan)
does not adding anything to the pallets array. What am I missing? It must be trivial but can not find mistake.
Upvotes: 1
Views: 49
Reputation: 57
var pallet is not initialized and it is an optional so when you append movementscan using ? , it wont be executed.
To fix this you have to some how initialize pallets array before appending to it . One way can be simply initialize with empty array :
var pallets = [MovementScan]()
Upvotes: 0
Reputation: 6067
Just change var pallets: [MovementScan]?
to
var pallets: [MovementScan] = [MovementScan]()
as @Carcigenicate you call append on nil value
Upvotes: 1