只在一个级别创建一个随机密钥

时间:2017-10-14 19:10:54

标签: json swift firebase

在Firebase中,可以使用.childByAutoId()

创建随机密钥
{
  "category" : {
    "random-key-generated-by-Firebase" : {
      "someValue" : true
    }
  }
}

这会导致像这样的json结构:

{
  "category" : {
    "random-key-generated-by-Firebase" : true
  }
}

我想知道是否有可能跳过最后一级并找到这样的结构呢?

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
    let dailyJobAssignments = JobsAndHabits.finalDailyJobsArray.filter({ return $0.assigned == User.finalUsersArray[section].firstName }).count 
    let weeklyJobAssignments = JobsAndHabits.finalWeeklyJobsArray.filter({ return $0.assigned == User.finalWeeklyJobsArray[section].firstName }).count 
    return dailyJobAssignments+weeklyJobAssignments
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
    let cell = tableView.dequeueReusableCell(withIdentifier: "customCell") as! JobSummaryCellB
    let dailyJobAssignments = JobsAndHabits.finalDailyJobsArray.filter({ return $0.assigned == User.finalUsersArray[indexPath.section].firstName })
    let weeklyJobAssignments = JobsAndHabits.finalWeeklyJobsArray.filter({ return $0.assigned == User.finalWeeklyJobsArray[indexPath.section].firstName })
    let jobAssignments = dailyJobAssignments + weeklyJobAssignments
    cell.userNameLabel.text = jobAssignments[indexPath.row].name
    return cell
}

1 个答案:

答案 0 :(得分:0)

对于swift,首先要创建一个关键参考

let key = ref.childByAutoId().key

然后创建一个childUpdate容器

let childUpdates = ["/category/\(key)": true]

最后更新

ref.updateChildValues(childUpdates)