Home > Back-end >  How to solve error message function "Expected expression error"
How to solve error message function "Expected expression error"

Time:04-24

I am working on a function the fetches data from a firebase database and displays it in a table view. The problem that I am having is that I keep getting the error message "Expected expression" and "Consecutive statements on a line must be separated by ';'"

func loadData() {
    self.reminders.removeAll()
    let ref = Database.database().reference()
    ref.child("usersEvents").observeSingleEvent(of: .value) { (snapshot) in
        if let reminderDict = snapshot.value as? [String: [String: Any]] {
            for (_,reminderElement) in reminderDict {
                print(reminderElement);
                let reminder = EntryViewController()
                reminder.event = reminderElement["event"] as? String
                reminder.dateAndTime = reminderElement["date and time"] as? String
                reminder.message = reminderElement["message"] as? String
                self.reminders.append(reminder)
                
            }
            
        }
        self.reminderList.reloadData()
        
    }) (error) in
    print(error.localizedDescription)
    
}

CodePudding user response:

Here's your code (abbreviated)

func loadData() {
    let ref = Database.database().reference()
    ref.child("usersEvents").observeSingleEvent(of: .value) { (snapshot) in
 
    }) (error) in
        print(error.localizedDescription)
}

note this

.observeSingleEvent(of: .value) <- the closing Parentheses ) in the wrong place

and it should be this

func loadData() {
    let ref = Database.database().reference()
    ref.child("usersEvents").observeSingleEvent(of: .value, with: { snapshot in
    }) { error in
        print(error.localizedDescription)
    }
}

noting no ) closing after .value and including the , with parameter with the closure

The idea is that everything between the ( this stuff ) is part of the .observe expression - so you want

        | - open paren                | - open closure
.observe( what type of observe, with: { a populated snapshot in 
    do some stuff with the snapshot 
} ) <- close paren
^ - close closure

CodePudding user response:

This is how my code looks now that I have made the chnages, is there something that I am not doing right?

func loadData() {
    self.reminders.removeAll()
    let ref = Database.database().reference()
    ref.child("usersEvents").observeSingleEvent(of: .value, with: { snapshot in
        if let reminderDict = snapshot.value as? [String: [String: Any]] {
            for (_,reminderElement) in reminderDict {
                print(reminderElement);
                let reminder = EntryViewController()
                reminder.event = reminderElement["event"] as? String
                reminder.dateAndTime = reminderElement["date and time"] as? String
                reminder.message = reminderElement["message"] as? String
                self.reminders.append(reminder)
            }) { error in
                    print(error.localizedDescription)
                }
            }
        
        self.reminderList.reloadData()
        
    }
  • Related