how to add information in an array with a class Swift

2442 views ios
0

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

answered question

2 Answers

3

You can try

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
        }

 }

posted this
11

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)" } }

posted this

Have an answer?

JD

Please login first before posting an answer.