flyer74
flyer74

Reputation: 195

how to add information in an array with a class Swift

I've got a problem for adding some informations in an array.

My class Flights is define by the following :

class Flight{
let date: String
let type: String
let regi: String

let totalTime: String

let depTime: String
let depPlace: String

let arrTime: String
let arrPlace: String

init(from dat: String, _ typ: String, _ reg: String, _ totaltim: String, _ depTim: String, _ depPlac: String, _ arrTim: String, _ arrPlac: String) {
    self.date = dat
    self.type = typ
    self.regi = reg

    self.totalTime = totaltim

    self.depTime = depTim
    self.depPlace = depPlac

    self.arrTime = arrTim
    self.arrPlace = arrPlac
}}

In my main code I've got declare my array like this :

var datas: [Flight] = []

And finally I've this code to add some informations coming from firebase : (I add some comment to show you what print() result)

if let user = Auth.auth().currentUser{
        // user is connect
        let ref = Database.database().reference()
        let userID = Auth.auth().currentUser?.uid

        let ev = ref.child("flights").child(userID!)

        ev.observe(.childAdded, with: { (snapshot) -> Void in
        let flightKey = snapshot.key

        ref.child("flights").child(userID!).child(flightKey).observeSingleEvent(of: .value) {(snapshot) in
            let value = snapshot.value as? NSDictionary

            let date = value?["Date"] as? String ?? "no date"
            let type = value?["aircraft-model"] as? String ?? "no type"
            let registration = value?["aircraft-registration"] as? String ?? "no callsign"
            let totalTime = value?["TOTAL-TIME"] as? String ?? "no total Time"
            let deppartTime = value?["departure-time"] as? String ?? "no departure Time"
            let deppartPlace = value?["departure-place"] as? String ?? "no departure Place"
            let arrivalTime = value?["arrival-time"] as? String ?? "no arrival Time"
            let arrivalPlace = value?["arrival-place"] as? String ?? "no arrival Place"

            print("Date : \(date) - type : \(type) - registration : \(registration) - Etc ...")// Give me exactly the value I requested


            self.datas.append(Flight(from: date, type, registration, totalTime, deppartTime, deppartPlace, arrivalTime, arrivalPlace))


            print(self.datas)// Give me "MyProjectName.Flight ...
            }

        })

    }else{
        // si non connecté alors DECONNEXION !!!!
        fatalError("error ...")
    }

So I don't understand why if I print the received value from firebase it work but if I print the array value which is completed by the firebase received value it didn't work ?

Thanks for your help !

Flyer-74

Upvotes: 1

Views: 90

Answers (3)

pbodsk
pbodsk

Reputation: 6876

Welcome :)

I think all is as expected and you're just seeing this because Swift doesn't know how to describe your objects.

To fix this, you should implement the CustomStringConvertible protocol in your Flight class (https://developer.apple.com/documentation/swift/customstringconvertible)

So something like

extension Flight: CustomStringConvertible {
    var description: String {
        var description = ""
        description.append("date: \(date)\n")
        description.append("type: \(type)\n")
        description.append("regi: \(regi)\n")
        //and so on
        return description
    }
}

Should give you what you are looking for.

Hope that helps you

Upvotes: 1

Chris Karani
Chris Karani

Reputation: 434

You could add a custom debug description for your object by adding an extension to Flight, and make it conform to the CustomDebugStringConvertible protocol. Conformance to this protocol requires that you provide a property: var debugDescription: String { get }. Inside this string is where you have full control over the debug values for your custom Object.

extension Flight: CustomDebugStringConvertible { var debugDescription: String { return "Date: \(date), Type: \(type), Registartion: \(regi)" } }

Upvotes: 1

Shehata Gamal
Shehata Gamal

Reputation: 100523

You can try to adopt CustomStringConvertible protocol

class Flight : CustomStringConvertible {

    var description: String {
        return "\(date) \(type)"  // add here any variable you want it to be printed 
    }

        let date: String
        let type: String
        let regi: String
        let totalTime: String
        let depTime: String
        let depPlace: String
        let arrTime: String
        let arrPlace: String
        init(from dat: String, _ typ: String, _ reg: String, _ totaltim: String, _ depTim: String, _ depPlac: String, _ arrTim: String, _ arrPlac: String) {
            self.date = dat
            self.type = typ
            self.regi = reg
            self.totalTime = totaltim
            self.depTime = depTim
            self.depPlace = depPlac
            self.arrTime = arrTim
            self.arrPlace = arrPlac
        }

 }

Upvotes: 1

Related Questions