0

Here is my data structure:

enter image description here

The problem is I don't know how to snap back the Data which is also the Key properly. What I want is only the Key and the value "true" are only markers. And I don't even know what type of data I am currently stored and how to snap the key value back.

What I want this that:

["BAFS", "Econ", "Geog"]

The following is how I write the data:

var subjectvalue: [String] = []
let structure = subjectvalue.reduce(into:[String:String](), { $0[$1] = "true"})
 let postRef = Database.database().reference().child("posts").child(id!).child("posts").setValue(structure)

I had been stocked for few days. Anyone have idea to help? Thank you in advance.

Frank van Puffelen
  • 565,676
  • 79
  • 828
  • 807
userPerPer
  • 35
  • 6

1 Answers1

0

You can get the dictionary from the DataSnapshot. Then get .keys of the dictionary and remove all elements which are not "true"

let postRef = Database.database().reference().child("posts").child("id").child("posts")
postRef.observe(.value) { snapshot in
    if let dict = snapshot.value as? [String : String] {
        let result = Array(dict.keys).removeAll(where: { $0 != "true" })
        print(result)
    }
}
RajeshKumar R
  • 15,445
  • 2
  • 38
  • 70