2017-03-08 30 views
0

所以目前我正在試圖從我的火力地堡的數據庫中提取數據,並將其設爲自己的變量,但每個圖表的孩子是一個特定的日期和時間(yy.mm .dd.hms)。所以我有一個數組存儲我需要的所有日期,但叫我的快照時,我不能引用它們。我已經試過這兩種方法哪個拋出錯誤「(孩子:)必須是一個非空字符串,不能包含‘’ '#' '$' '[' 或 ']''」接聽火力地堡快照從一個孩子的數組SWIFT

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 
       } 
      } 
     } 
    } 
} 

回答

0

嘗試這個

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

然後打印「後」,你會得到[ 「170802120618」, 「170802101427」]

2

權。你希望子鍵是一個自動生成的散列值。您可以使用childByAutoId()創建這些。另外,如果我是你,我只想商店,日期爲字符串,並根據需要解析的。下面的東西將是一個例子:

Posts { 
    -Kebfdajksthm { 
     first_name: "first", 
     post_date: "yymmddhhmmss" 
    } 
} 
+0

我明白你的意思,但是我會如何引用它?我試圖爲一個以上的用戶這樣做,所以我不能使用currentUser或currentChild。 – itsTheJaguar