Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Firebase with Swift ambiguous use of observeEventType

I've been pulling my hair out because of this. Going to all the pages with related incidents and multiple tutorials I find nothing wrong with my code here, but somehow it only doesn't fail if I print out the values (which works) or assign them as! NSArray which then gives me an empty array.

a print of snapshot.value shows

( friend1, 
  friend2, 
  friend3 
)

I've tried snapshot.value.values ... no dice. I've tried playing with the unwrapping ... no dice. and this is my final attempt:

    friendsList = [String]()

    ref.observeSingleEventOfType(.Value) { (snapshot) -> Void in
        if snapshot.value is NSNull {

        } else {
            for child in snapshot {
                self.friendsList.append(child.value)
            }
        }
    }

which gives me the ambiguous thing again.

like image 503
Aylii Avatar asked Dec 11 '22 17:12

Aylii


1 Answers

Just some coding errors

Remove: (snapshot)-> Void

Change: child in snapshot as snapshot is not a sequence, whereas snapshot.children is

I assume you want to store the friends name as a string and name is a key in your structure. So change self.friendsList.append(child.value) to

let name = child.value["name"] as? String
friendsList.append(name!)

Here's the corrected code:

var friendsList = [String]()

ref.observeSingleEventOfType(.Value, withBlock: { snapshot in

    if snapshot.value is NSNull {

    } else {
        for child in snapshot.children {
            let name = child.value["name"] as? String
            friendsList.append(name!)
        }
        print("\(friendsList)")
    }
})
like image 105
Jay Avatar answered Jan 28 '23 15:01

Jay