iOS Swift - 在Firebase

时间:2017-09-25 05:41:19

标签: ios swift firebase firebase-realtime-database

我正在制作出勤应用,并且我试图显示日期范围内的登录时间,退出时间和休息时间等值。例如,用户选择从2017年9月23日到9月25日的日期,应用程序应显示这些日期的登录时间,注销时间,休息时间等列表。

这是我的代码:

    databaseHandle = ref.child("admin").child(userID!).child("employees").child(self.employeeInfo!).child("attendance").queryOrdered(byChild: fromDateTimeStamp!).queryEnding(atValue: toDateTimeStamp).observe(.childAdded, with: { (snapshot) in

        for item in snapshot.children {
            let snap = item as! DataSnapshot
            let key = snap.key
            let value = snap.value

            print("key: \(key), value: \(value!)")
        }

    })

    ref.removeObserver(withHandle: databaseHandle!)

我选择了2017年9月23日至9月25日,因为在2009年9月24日没有登录和退出,这是我在尝试进行查询时得到的结果:

    key: breakFinish, value: 12:17 PM
    key: breakStart, value: 11:55 AM
    key: currentDate, value: 9/23/2017
    key: login, value: 11:51 AM
    key: logout, value: 1:00 PM
    key: scanCount, value: 4
    key: breakFinish, value: 10:19 AM
    key: breakStart, value: 10:19 AM
    key: currentDate, value: 9/25/2017
    key: login, value: 10:18 AM
    key: logout, value: 10:21 AM
    key: scanCount, value: 4

现在,我的问题是我如何遍历snapshot.children或访问单个值来使用它?因为我要将这些数据导出到csv文件中。所以,当我这样做时,我希望它显示如下:

    Date          Login        Break Start     Break Finish      Logout
    9/23/2017     11:51 AM     11:55 AM        11:55 AM          1:00 PM
    9/25/2017     11:51 AM     11:55 AM        11:55 AM          1:00 PM

我试过这个,但是当它无法找到值时会出错。

    let attendanceDict = snapshot.value as? [String:AnyObject]

        if let actualAttendanceDict = attendanceDict {

            for each in actualAttendanceDict {

                let dateKey = each.key
                self.dateKeyArray += [dateKey]
                let dateLogin = each.value["currentDate"] as! String
                self.dateLoginArray += [dateLogin]
                let timeInTime =  each.value["login"] as! String
                self.loginTimeArray += [timeInTime]
                let timeOutTime = each.value["logout"] as! String
                self.logoutTimeArray += [timeOutTime]
                let breakStartTime = each.value["breakStart"] as! String
                self.breakStartTimeArray += [breakStartTime]
                let breakFinishTime =  each.value["breakFinish"] as! String
                self.breakFinishTimeArray += [breakFinishTime]



            }
        }

请帮忙。谢谢!

2 个答案:

答案 0 :(得分:1)

诀窍是转换为里面的词典 for循环:

def home(request):
    context = {}
    template = 'home.html'
    return render(request,template,context)

def about(request):
    context = {}
    template = 'about.html'
    return render(request,template,context)

答案 1 :(得分:0)

使用Frank的答案作为参考,我刚刚从代码中删除了for循环并且它有效。

    databaseHandle = ref.child("admin").child(userID!).child("employees").child(self.employeeInfo!).child("attendance").queryOrdered(byChild: fromDateTimeStamp!).queryEnding(atValue: toDateTimeStamp).observe(.childAdded, with: { (snapshot) in


         let attendanceDict = snapshot.value as? [String:AnyObject]

         let dateLogin = attendanceDict?["currentDate"] as? String
         self.dateLoginArray += [dateLogin]
         let timeInTime =  attendanceDict?["login"] as? String
         self.loginTimeArray += [timeInTime]
         let timeOutTime = attendanceDict?["logout"] as? String
         self.logoutTimeArray += [timeOutTime]
         let breakStartTime = attendanceDict?["breakStart"] as? String
         self.breakStartTimeArray += [breakStartTime]
         let breakFinishTime =  attendanceDict?["breakFinish"] as? String
         self.breakFinishTimeArray += [breakFinishTime]

  })