从iOS / Swift 3中的FirebaseDatabase中提取随机键/值

时间:2017-05-13 16:04:53

标签: ios json firebase swift3

我在Firebase中创建了一个简单的数据结构:

{
  "authors": {
    "John Lennon": [
      "Life is what happens when you're busy making other plans.",
      "Time you enjoy wasting, was not wasted."
    ],
    "Steve Jobs": [
      "Design is not just what it looks like and feels like. Design is how it works.",
      "Stay hungry, stay foolish."
    ]
  }
}

我想随机选择一个作者,然后从该作者中选择一个随机引用。到目前为止我只有这个代码,它只返回我手动指定的用户的所有引号。

func loadQuoteFromDatabase()  {

    var ref: FIRDatabaseReference!
    ref = FIRDatabase.database().reference()

    ref.child("authors/John Lennon/").observeSingleEvent(of: .value, with: { (snapshot) in

        print(snapshot.value ?? "")

    }) { (error) in
        print (error.localizedDescription)
    }
}

回报如下:

(
    "Life is what happens when you're busy making other plans.",
    "Time you enjoy wasting, was not wasted."
)

我不确定如何从括号中取出一个项目。

我是这种编码的业余爱好者,所以我发现这很容易解决。任何想法我如何抓住一个随机作者,然后随机引用他们的?

感谢。

0 个答案:

没有答案