1

I have developed my iOS app and I have a question about using firebase.

I've already read this question.

How to convert document to a custom object in Swift 5?

However, I have a different question.

I've got document data from firebase cloud store. and I've been trying convert the data to my custom object.

Here is my object.

struct Car: Codable{
var car_id : String
var user_id : String
var alias : String
var car_number : String
var trip_summary : [String : Any]
var create_time : Date
var update_time : Date
}

My problem is how to map trip_summary to my custom object.

I made subclass for my trip_summary.

class Trip_Summary: Codable {
    var total_distance: Int
    var total_time: CLong
    var avg_mileage: Float
    var avg_score: Float
    var last_trip_id: String
    var last_trip_distance: Int
    var last_trip_time: CLong
    var last_fuel_mileage: Float
    var last_driving_score: Int
    var best_mileage: Float
    var best_score: Int
}

But I couldn't convert trip_summary.

What I've tried.

  1. We created a decodable object and declared it instead of Any type.
  • There was no detail on how to do this, so I tried it and gave up.
  1. Declared and converted to TripSummary format.
  • This causes the document to return nil.
  1. Just change everything to string and declare one by one.
  • This is not a good programming method.

Here is my firebase

Image-1

Image-2

Please understand that I cannot disclose all codes due to security issues. Thanks.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
Jiogenes
  • 71
  • 1
  • 4

0 Answers0