Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/database/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Swift Firebase数据库获取数据_Swift_Database_Firebase_Firebase Realtime Database - Fatal编程技术网

Swift Firebase数据库获取数据

Swift Firebase数据库获取数据,swift,database,firebase,firebase-realtime-database,Swift,Database,Firebase,Firebase Realtime Database,我有Firebase云结构: 如何获取firstBook和secondBook的名称并写入数组 并从firstbook 我想试试这个: var array: [String] = [] override func viewDidLoad() { super.viewDidLoad() Database.database().reference().ref?.child("category").child("book").observe(.value, with: { (sna

我有Firebase云结构:

如何获取
firstBook
secondBook
的名称并写入数组

并从
firstbook

我想试试这个:

var array: [String] = []

override func viewDidLoad() {
    super.viewDidLoad()
    Database.database().reference().ref?.child("category").child("book").observe(.value, with: { (snapshot) in
        let valueCat = snapshot.value as! NSDictionary

        let username = valueCat["name"] as? String ?? ""
        print(username)
        self.array.append(username)
    }
}
到另一个阵列:

var quotAarray: [String] = []

override func viewDidLoad() {
    super.viewDidLoad()
    Database.database().reference().ref?.child("category").child("book").child("firstBook").observe(.value, with: { (snapshot) in
        let valueCat = snapshot.value as! NSDictionary

        let quote = valueCat["Quotes"] as? String ?? ""
        print(quote)
        self.quotAarray.append(quote)
    }
}
试试这个:

 let valueCat = snapshot.value as! NSDictionary
 if let firstBook = valueCat["firstBook"] as? NSDictionary {
     print(firstBook["name"])
 }

要想得到所有的书名,你必须反复阅读孩子们的书名,检查这是否对你有帮助。我根据你收到的回复制作了一本示例词典

使用的代码:

/// Sample Data on Base of your Output SHown
var DBData = [["firstBook":["Quotes": ["Quote1":111, "Quote2":222, "Quote3":333 ], "name":"first_Book"]], ["SecondBook":["Quotes":["Quote1":111,"Quote2":222,"Quote3":333],"name":"Second_Book"]]]

/// Names Array - In which book names need to be added
var nameArray = [String]()

/// For loop on base of DBData as it Array of Array Data
for i in 0..<DBData.count {
    /// Assumed You have only one Main key in each array
    /// Can be known before .
    /// if you do not know Let's get keys here in Array at index i
    let keysArray : [String] = Array(DBData[i].keys)

    /// Now get the current dict value on base of key retrieved as in
    /// first case its - firstBook
    /// Second case its - SecondBook
    let currentDictValue = DBData[i][keysArray[0]]

    /// Now get Names of books
    /// name is the key used in your DB its generic and its fixed
    /// Now get value and add in Array
    nameArray.append(currentDictValue!["name"] as! String)
}

/// Output
print("Name Array:\(nameArray)")
///基于显示的输出的示例数据
var DBData=[“第一本书”:[“引号”:[“Quote1”:111,“Quote2”:222,“Quote3”:333],“名称”:“第一本书”],[“第二本书”:[“引号”:[“Quote1”:111,“Quote2”:222,“Quote3”:333],“名称”:“第二本书”]]
///名称数组-需要在其中添加图书名称
变量nameArray=[String]()
///基于DBData的For循环作为数组数据的数组

对于iOS极客中的i..@iOS极客,但您的代码如何与Firebase一起工作?

问题出在哪里?你没有用户名吗?@iOSGeek,是的,我无法获取username@iOSGeek,如果我打印(snapshot.value),它会给我:
可选({firstBook={Quote1=1111;Quote2=222;Quote3=333;};name=firs;};name=book;secondBook={Quote={Quote1=111;Quote2=222;Quote3=333;};name=secondBook;};};})
@iOS极客,你能帮我吗?也许我能在某个地方读到它?好的,我会调查它,需要正确取名字吗?通过所有的孩子?我能从所有的书中取名字吗(第一本书+第二本书)?并制作通用代码,例如,如果我去电影放映所有电影的名称?书籍、电影的名称…不是静态的,它们总是会增加。我可以这样做吗?