Author Topic: IOS + Swift + Firebase Question  (Read 1291 times)

0 Members and 1 Guest are viewing this topic.

Offline KaramelTopic starter

  • Regular Contributor
  • *
  • Posts: 178
  • Country: tr
IOS + Swift + Firebase Question
« on: July 28, 2017, 12:18:37 am »
Hi everyone,

I know you think that it is weird when you see the title in Electronic forum. However, I am electrical and electronics engineering student and I am, unfortunately, dealing with IOS programing. I must read data from Firebase cloud database storage. Let's see what I did.

I created JSON database tree and I can read specific values with these codes. I can see on table view "Albert Einstein"

Code: [Select]
ref.child("Personel").child("Name").observeSingleEvent(of: .value, with: { (snapshot) in
           
            if let item = snapshot.value as? String{
               
                self.myList.append(item)
                self.LessonsTableView.reloadData()
            }
           
        })




But, I want to see which categories under Personal column? Like this,



Is there any way to get or learn which columns are under "Personal"


P.s: Table view output must be -> Age, Name, Photo
 

Offline sokoloff

  • Super Contributor
  • ***
  • Posts: 1799
  • Country: us
Re: IOS + Swift + Firebase Question
« Reply #1 on: July 28, 2017, 11:56:25 am »
It seems like your questions is a straight JSON handling in Swift question.

See here:
https://developer.apple.com/swift/blog/?id=37

In particular:
Code: [Select]
for (key, value) in ref {
// access all key / value pairs in dictionary (which is called "ref" in your code snippet)
}
 

Offline KaramelTopic starter

  • Regular Contributor
  • *
  • Posts: 178
  • Country: tr
Re: IOS + Swift + Firebase Question
« Reply #2 on: July 28, 2017, 07:20:40 pm »
All examples,

They know "Age", "Name" or "Photo" and they reach their values. But think that, I don't know "Age".  It could be "AGE", "Age:", "Age=" something like theses.

I just want to look at under of "Personel" with programmatically and I must find "Age", "Name" and "Photo". Just these, not their values.
 

Offline sokoloff

  • Super Contributor
  • ***
  • Posts: 1799
  • Country: us
Re: IOS + Swift + Firebase Question
« Reply #3 on: July 28, 2017, 09:05:19 pm »
That's what the "for (key, value) in ref" is for.

See below for a more digested answer:
Code: [Select]
let jsonObject: [String: [String:Any]] = [
    "Personel": [
        "age": 27,
        "name": "Albert Einstein",
        "picture": "whatever..."
    ]
]

print("\nHere's all the data (for a set depth tree)")
for (key, value) in jsonObject {
    print("Key:\(key)")
    for (innerkey, innervalue) in value {
        print("\(key)->\(innerkey):\(innervalue)")
    }
}

print("\nHere's exactly what you're asking for I think")
for (key, value) in jsonObject {
    //print("Key:\(key)")
    for (innerkey, innervalue) in value {
        print("\(innerkey)")
    }
}

 

Offline sokoloff

  • Super Contributor
  • ***
  • Posts: 1799
  • Country: us
Re: IOS + Swift + Firebase Question
« Reply #4 on: July 28, 2017, 09:08:01 pm »
Or, if you only want to dive into keys that are "Personel" then:

Code: [Select]
let jsonObject: [String: [String:Any]] = [
    "Personel": [
        "age": 27,
        "name": "Albert Einstein",
        "picture": "whatever..."
    ]
]

print("\nHere's all the data (for a set depth tree)")
for (key, value) in jsonObject {
    print("Key:\(key)")
    for (innerkey, innervalue) in value {
        print("\(key)->\(innerkey):\(innervalue)")
    }
}

print("\nHere's exactly what you're asking for I think")
for (key, value) in jsonObject {
    //print("Key:\(key)")
    if (key == "Personel") {
        for (innerkey, innervalue) in value {
            print("\(innerkey)")
        }
    }
}
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf