lundi 27 février 2017

Append to array inside if statement Swift 3

I am trying to append to an array inside an if statement, I already declared the array outside of the if statement var AnArray = [String]() But when I try to append to the array inside an if statement: self.AnArray.append("hello") and then print it outside of the if statement, print (AnArray).However, when I print the array inside the if statement, it is able to append. How can I solve this? Thanks!

ref.observeSingleEvent(of: .value, with: { (FIRDataSnap) in
        for child in FIRDataSnap.children.allObjects {
            let key = (child as AnyObject).key as String
            self.myArray.append(key)
        }




        for (_, element) in self.myArray.enumerated() {

            self.ref.child(element).child("Players").observeSingleEvent(of: .value, with: { (Snap) in

                if Snap.childrenCount < 2 {
                    self.CanJoinArray.append("hello")

                }
                else {
                    print("Can't join lobby\(element)... Full!")

                }


            })



        }

       self.JoinSession()

    })




}

Aucun commentaire:

Enregistrer un commentaire