1

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

3 Answers 3

1

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

Sign up to request clarification or add additional context in comments.

7 Comments

Hi ! It work very good but How can I use the value to add it on a label ? I've been tried like this : dateLabel.text = datas[indexPath.row].date but nothing appears ...
@flyer74 interesting...that should work. Can you show me where you try to add the text? I guess it is in a tableview since you are referring to an indexPath. Again, maybe try adding a breakpoint to that line and see what your datas array contains at that point.
Affirm I'm trying to fil a tableview so the complete code is placed in the : override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) function. the complete code line to show the date is cell.dateLabel.text = datas[indexPath.row].date. I'll try with a break point just now
That crazy ! The control doesn't go into the cellForRow function when the data array is completed by informations coming from my class Flight... But when I manually add some informations :var datas: [Flight] = [Flight(from: "21-10-18", "C172", "F-GIRQ", "01:00", "13:00", "LFLK", "14:00", "LFHS")] in my array this working good ...
Maybe your problem is, that at the time when your tableview is being filled, the data is not yet ready, meaning that when cellForRowAt is called, there is no content for it to show. In your code above it seems that the retrieval of data is done asynchronously, meaning that it is not ready when the tableview starts calling cellForRowAt. To fix that, call tableView.reloadData() when you're done populating your array to tell the tableview to re-render its content.
|
1

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
        }

 }

Comments

1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.