从具有SWIFT

时间:2017-03-08 03:17:11

标签: ios arrays swift firebase firebase-realtime-database

所以我目前正在尝试从Firebase数据库中获取数据并将其设置为自己的变量,但每个图表的子项都是特定的日期和时间(yy.mm.dd.h.m.s)。所以我有一个存储我需要的所有日期的数组,但在调用我的快照时我无法引用它们。我已经尝试了这两种方法抛出错误“(child :)必须是非空字符串而不包含'。' '#''$''['或']'''

var postCollection = [170802120618, 170802101427] //yy.mm.dd.hh.mm.ss   

ref.child("users").child(uid!).child("Posts").child(self.postCollection[indexPath.row]).observe(.value, with: { (snapshot) in
                for item in snapshot.children{
                    let snapshotValue = snapshot.value as? NSDictionary
                    let firstNameSnap = snapshotValue?["First Name"] as? String ?? ""
    currentCell.nameLabel.text = firstNameSnap
        }
    })

var postCollection = [170802120618, 170802101427]  //yy.mm.dd.hh.mm.ss  
let selection = self.postCollection[indexPath.row]
ref.child("users").child(uid!).child("Posts").child(self.postCollection[indexPath).observe(.value, with: { (snapshot) in
                for item in snapshot.children{
                    let snapshotValue = snapshot.value as? NSDictionary
                    let firstNameSnap = snapshotValue?["First Name"] as? String ?? ""
    currentCell.nameLabel.text = firstNameSnap
        }
    })

数据库图表大致是:

FIR{
    users{
        uid{
            username: UserName
            Posts{
                170802120618{
                    First Name: first
                }
            }
        }
    }
}

2 个答案:

答案 0 :(得分:2)

右。您希望子键是自动生成的哈希值。您可以使用a创建这些内容。如果我是你,我只会将日期存储为字符串并根据需要解析它们。下面的内容就是一个例子:

childByAutoId()

答案 1 :(得分:0)

试试这个

var post = [String]()
ref.observe(.value, with: { (snapshot) in
            for item in snapshot.children{
                self.post.append((item as AnyObject).key)
        }
    })

然后你打印“post”,你会得到 [“170802120618”,“170802101427”]