Home > other >  No exact matches in call to instance method 'append', macOS swift with Firebase
No exact matches in call to instance method 'append', macOS swift with Firebase

Time:06-24

I´m new in apple development and I´m trying to populate NSTableView with struct from Firebase Firestore, But I have an error in appending the data

Var costumerArray: [CostumerData]()

func numberOfRows(in tableView: NSTableView) -> Int {
    return costumerArray.count
}

func tableView(_ tableView: NSTableView, objectValueFor tableColumn: NSTableColumn?, row: Int) -> NSTableCellView?{

    let cell = tableView.makeView(withIdentifier: tableColumn!.identifier, owner: self)as? NSTableCellView {
        let costumer = costumerArray[row]
        cell.textField?.stringValue = costumer.fullName
        return cell
    }
    return nil

}

This is the method that I make the call to firebase and it works.

func loadCostumers(){
    db.collection(self.collectionName).addSnapshotListener { (QuerySnapshot, error) in
        self.costumerArray = []
        if let error = error{
            print("Error geting documents,\(error)")
        } else {
            for document in QuerySnapshot!.documents{
                print("\(document.documentID) => \(document.data())"). // here the data printed in console
                let data = document.data()
                if let costumerName = data["fullName"] as? String, let costumerNumber =
                    data["phoneNum"] as? String {
                    let newCostumer = Costumers(id: document.documentID,fullName: costumerName, phoneNum: costumerNumber, email: "", address: "", profession: "", age: "", dateOfBirth: "")

                    self.costumerArray.append(newCostumer) // Here I need Help with appending data to NSTableView

                    DispatchQueue.main.async {
                        self.tableView.reloadData()
                    }
                }
            }
        }

    }
}

Here is a Screenshot of the error

CodePudding user response:

First of all you are not declaring the array correctly

var costumerArray = [CostumerData]() 
// or
var costumerArray: [CostumerData] = []

Second thing is that your "costumerArray" seems to be declared as [CostumerData] and you are trying to append Costumers object to it. If these types are not related at all the compiler will get confused and it is probably the reason for this error. You might want to change the type of costumerArray to the following:

var costumerArray = [Costumers]()

Here are some resources for learning how to style your Swift code:

But to learn more on how to use Swift most efficiently I would strongly suggest to take a look a the official Swift Language Guide and preferably reading the whole thing. It's incredibly informative!

  • Related