Swift 错误 "Type 'BookingDataModel' does not conform to protocol 'Decodable'"

Swift Error "Type 'BookingDataModel' does not conform to protocol 'Decodable'"

我想按照客户的要求使我的应用程序的某些部分离线。我一直在绞尽脑汁使我的结构可编码,以便我可以将数据保存为用户默认值,但我总是收到此错误

Type 'BookingDataModel' does not conform to protocol 'Decodable'

这是我的 BookingDataModal:

struct BookingDataModel: Codable {

    static var shared = BookingDataModel()

    var collaps = false
    var _id = ""
    var userId = ""
    var venderName = ""
    var created_at = ""
    var transaction_id = ""
    var payment_method = ""
    var updated_at = ""
    var booking_confirmed = false
    var booking_status = ""
    var payment_status = false
    var type = ""
    var isCanceledRequest: Bool?

    var flight: [FlightBookingDataModel]?
    var newFlight: (departure: [FlightBookingDataModel], return: [FlightBookingDataModel])?
    var event: HotelBookingDataModel?
    var hotel: NewHotelBookingDataModel?//HotelBookingDataModel?

    var isAxaPolicy = false
    var relationshipId = ""
    var passengers = [UsersModel]()
    var total = 0.0
    var tacCoinDiscount: Int?
    var couponDiscount: Int?
    var afterDiscountAmount: Int?

     func getBookingDataModel(arr: NSArray, type: String) -> [BookingDataModel] {
    // save data from API
    }
    }

起初我以为我的其他模态是不可编码的,所以我把它们都变成了可编码的,它仍然会抛出同样的错误 这是 FlightBookingDataModal:

struct FlightBookingDataModel: Codable {

    static var shared = FlightBookingDataModel()

    var pnrData = PNRDataModel()
    var src_name = ""
    var dst_name = ""
    var srcCode = ""
    var dstCode = ""

    var atime_utc = String()
    var dtime_utc = String()
    var atime = String()
    var dtime = String()

    var flight_no = ""
    var checkin = ""
    var price = 0.0
    var airLineName = ""
    var eticket_link = ""
    var invoice = ""
    var isReturnFlight = false

    func getFlightBookigDataModel(dict: NSDictionary) -> [FlightBookingDataModel] {
    // Save data from API
    }
    }

PNR 模式:

class PNRDataModel: Codable  {
  var src = ""
  var dst = ""
  var iata = ""
  var pnr = ""
  }

酒店预订数据模型:

struct HotelBookingDataModel: Codable {

    var cityName = ""
    var name = ""
    var address = ""
    var startOfServiceTime = ""
    var endOfServiceTime = ""
    var aetherReference = ""
    var provider = ""
    var reference = ""
    var price = 0.0
    var date = ""
    var reservationId = ""
    }

新酒店预订数据模型:

struct NewHotelBookingDataModel: Codable {
    var name = ""
    var address = ""
    var cityName = ""
    var price = 0.0
    var reference = ""
    var provider = "" //not found in new response
    var startOfServiceTime = ""
    var endOfServiceTime = ""
    var aetherReference = ""
    }

如果有人能给我任何关于为什么这不起作用的见解,那将对我有很大帮助。我只想将 [BookingDataModal] 离线保存一段有限的时间,直到用户重新在线。

错误的原因是BookingDataModel中的属性newFlight

Codable 不支持元组。