我有以下结构:
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)
不会向托盘数组添加任何内容。我错过了什么?一定是小事,但找不到错误。 最佳答案
只需更改var pallets: [MovementScan]?
到var pallets: [MovementScan] = [MovementScan]()
作为@Carcigenicate,您可以调用append on nil value
关于arrays - 将数组值附加到结构,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/50337979/