Firebase - 未返回正常计数值的NSObject数组

时间:2016-10-10 09:42:45

标签: ios swift for-loop firebase firebase-realtime-database

我正在尝试查询特定公司下的用户,然后将其附加到NSObjects数组。

代码:

var userDetails = [UserDetails]()
func getDriversInfoWithCompany() {

      FIRDatabase.database().reference().child(“users").queryOrderedByChild("company").queryEqualToValue(“KFC").observeEventType(.ChildAdded, withBlock: { (snapshot) in

        let details = UserDetails(key: snapshot.key, snapshot: snapshot.value  as! Dictionary<String, AnyObject>)

        self.userDetails.append(details)

    self.loadStaffs()                

     }, withCancelBlock: nil)

}

func loadStaffs() {

    let count = self.userDetails.count

    print(count)

    for i in 0...count {

        print(self.userDetails[i].username)

    } 

}

我正在使用for循环来获取它的用户名。但是我遇到了错误fatal error: Index out of range然后我print() count并输出了 1 2 3 4 而不只是4。我想我怎么能得到最终值4所以如果我循环它不会给我一个错误。谢谢!

编辑:

Code:
    Snap (NU4chDtG7eWKXgPaTuFFDfWXWcf2) {
    company = "KFC";
    email = "user@email.com";
    fileUrl = "https://firebasestorage.googleapis.com/v0/b/myapp-234esg.appspot.com/o/NU4chDtG7eWKXgPaTuFFDfWXWcf2%2F492003486?alt=media&token=34cecf1e-d65f-4856-8aac-3afdcf9d451e";
    userRole = DISPATCH;
    username = UserName;
    }
    Snap (blkSE7N8GEPeoObNCWLhjyUnkSu2) {
    company = "KFC";
    email = "user@email.com";
    fileUrl = "https://firebasestorage.googleapis.com/v0/b/myapp-234esg.appspot.com/o/profileImage%2FblkSE7N8GEPeoObNCWLhjyUnkSu2?alt=media&token=b97fd727-12f8-4725-a830-67133de78aab";
    userRole = SHIPPER;
    username = UserName;
    }
    Snap (d605A6Mc0hRWygvK9fjwUnPfliU2) {
    company = "KFC";
    email = "user@email.com";
    fileUrl = "https://firebasestorage.googleapis.com/v0/b/myapp-234esg.appspot.com/o/profileImage%2Fd605A6Mc0hRWygvK9fjwUnPfliU2?alt=media&token=f35347ca-8da5-402d-a6b6-c3a56f59cbcf";
    userRole = DRIVER;
    username = UserName;
    }
    Snap (fGfIKyF8g5WAOLBdc5dpNSWmaGy2) {
    company = "KFC";
    email = "user@email.com";
    fileUrl = "https://firebasestorage.googleapis.com/v0/b/myapp-234esg.appspot.com/o/profileImage%2FfGfIKyF8g5WAOLBdc5dpNSWmaGy2?alt=media&token=79735c4e-aa70-4cb0-8a16-b6efedb668a2";
    userRole = DISPATCHER;
    username = "UserName";
    }

代码:

Snap (users) {

NU4chDtG7eWKXgPaTuFFDfWXWcf2 =     {

company = "KFC";

email = "user@email.com";

fileUrl = "https://firebasestorage.googleapis.com/v0/b/myapp-234esg.appspot.com/o/NU4chDtG7eWKXgPaTuFFDfWXWcf2%2F492003486?alt=media&token=34cecf1e-d65f-4856-8aac-3afdcf9d451e";

userRole = DISPATCH;

username = UserName;

};

blkSE7N8GEPeoObNCWLhjyUnkSu2 =     {

company = "KFC";

email = "user@email.com";

fileUrl = "https://firebasestorage.googleapis.com/v0/b/myapp-234esg.appspot.com/o/profileImage%2FblkSE7N8GEPeoObNCWLhjyUnkSu2?alt=media&token=b97fd727-12f8-4725-a830-67133de78aab";

userRole = SHIPPER;

username = UserName;

};

d605A6Mc0hRWygvK9fjwUnPfliU2 =     {

company = "KFC";

email = "user@email.com";

fileUrl = "https://firebasestorage.googleapis.com/v0/b/myapp-234esg.appspot.com/o/profileImage%2Fd605A6Mc0hRWygvK9fjwUnPfliU2?alt=media&token=f35347ca-8da5-402d-a6b6-c3a56f59cbcf";

userRole = DRIVER;

username = UserName;

};

fGfIKyF8g5WAOLBdc5dpNSWmaGy2 =     {

company = "KFC";

email = "user@email.com";

fileUrl = "https://firebasestorage.googleapis.com/v0/b/myapp-234esg.appspot.com/o/profileImage%2FfGfIKyF8g5WAOLBdc5dpNSWmaGy2?alt=media&token=79735c4e-aa70-4cb0-8a16-b6efedb668a2";

userRole = DISPATCHER;

username = "UserName";

};

}

第一个是.ChildValue,第二个是.Value。谢谢!

1 个答案:

答案 0 :(得分:1)

尝试将 loadStaff() 功能更改为: -

func loadStaffs() {

        let count = self.userDetails.count
        print(count)

            for i in 0...count-1 {

             print(self.userDetails[i].username)

             } 
         }

用于检索用途: -

func getDriversInfoWithCompany() {

  FIRDatabase.database().reference().child(“users").queryOrderedByChild("company").queryEqualToValue(“KFC").observeEventType(.Value, withBlock: { (snapshot) in
    if let snapDict = snapshot.value as? [String:AnyObject]{ 

      for each in snapDict as [String:AnyObject]{

      let details = UserDetails(key: each.0, snapshot: each.1  as! Dictionary<String, AnyObject>)

     self.userDetails.append(details)

     self.loadStaffs()                

   } 
  }
 }, withCancelBlock: nil)

}