2
votes

Here’s my code:

func loadData() {
    ref.child(currentUserID!).observe(.childAdded) {
        (snapshot) in
        let snapshotValue = try? snapshot.value as? [String: AnyObject]
        if let item = try TableViewModel(id: snapshot.key, likeLabel: self.likeLabel, playLabelString: self.playLabelString, json: snapshotValue) {
            self.items.append(item)
        }            
        self.tableViewModel = self.items.reversed() as [TableViewModel]
    }
}

I can’t get my head around the following error:

Invalid conversion from throwing function of type (_) throws -> () to non-throwing function type(DataSnapshot) -> Void

in the following line:

ref.child(currentUserID!).observe(.childAdded) {

Please help.

1

1 Answers

3
votes

Your main issue is the following expression:

if let item = try TableViewModel(...) {

the try command indicates your Firebase completion closure might throw an error, a situation not expected by the observe API you are passing your closure to, hence the compiling error you are having.

Ok, with that out of the way, I belive you had the try? operator in mind. If so, try this instead:

if let item = try? TableViewModel(...) {

or simply:

if let item = TableViewModel(...) {

if the TableViewModel doesn't throw any error at all (i.e. it's just a failable initializer instead).

By the way, your snapshotValue variable could also use some help:

let snapshotValue = snapshot.value as! [String: AnyObject]