Lukas Bimba
Lukas Bimba

Reputation: 826

Swift - 2 empty string arrays

Trying to find out why sometimes this happens in swift/Xcode. When I check "employees.isEmpty" is returns correctly because it is empty. When is check "uids.isEmpty" it return empty as well but for some reason it thinks there are values and then executes my firebase call which of course crashes the app because the array is nil. Why does this happen? Same code. Same set up. Yet "employees.isEmpty" returns nil like it should (in my testing) and the other "uids.isEmpty" is empty but executes like there are values when there are not.

if employees.isEmpty == false {
    print("This shouldnt be called 1")
    OneSignal.postNotification(["contents": ["en": "\(message)"],
                                "include_player_ids": employees,
                                "headings": ["en": "Hey \(businessName)"],
                                "ios_badgeType": "Increase",
                                "ios_badgeCount": 1])
} else {
    print("Empty no execution")
}
print(employees)
print("Checking inside employees here")
print(uids)
print("Checking inside uids here")

if uids.isEmpty == false {
    print("This shouldnt be called 2")
    let pathing = ref.child("notification").child(uids).childByAutoId()
    pathing.setValue(values)
} else {
    print("Empty no execution")
}

They both print out the same when no values are assigned, just like this: "[]".

Declarations/Appending

var data:[String] = []
var dataUID:[String] = []


// GETTING THE ONESIGNAL ID'S FROM THE EMPLOYEES FOR NOTIFICATIONS
func getEmployees() {
    Database.database().reference().child("Businesses").child(self.otherUser?["uid"] as! String).child("registered_employees").observe(.childAdded, with: { (snapshot) in
        if snapshot.exists() {
           let data = snapshot.value as? NSDictionary
            let uid = data?["onesignal"] as? String
            self.data.append(uid!)
            print(self.data)
        } else {
            print("didnt call right values")
        }
    })
}

func getEmployeesUIDs() {
    Database.database().reference().child("Businesses").child(self.otherUser?["uid"] as! String).child("registered_employees").observe(.value, with: { (snapshot) in
        if snapshot.exists() {
            let data = snapshot.value as? NSDictionary
            let uid = data?["uid"] as? String
            self.dataUID.append(uid!)
            print(self.dataUID)
            print("Printing the employee uids right here")
        } else {
            print("didnt call right values")
        }
    })
}


let employees = self.data
let uids = self.dataUID.description

if employees.isEmpty {
                print("Empty no execution")
            } else {
                print("This shouldnt be called 1")
                OneSignal.postNotification(["contents": ["en": "\(message)"],
                                            "include_player_ids": employees,
                                            "headings": ["en": "Hey \(businessName)"],
                                            "ios_badgeType": "Increase",
                                            "ios_badgeCount": 1])
            }
            print(employees)
            print("Checking inside employees here")
            print(uids)
            print("Checking inside uids here")

            if uids.isEmpty {
                print("Empty no execution")
            } else {
                print("This shouldnt be called 2")
                let pathing = ref.child("notification").child(uids).childByAutoId()
                pathing.setValue(values)

            }

Upvotes: 0

Views: 334

Answers (1)

Mohammad Yunus
Mohammad Yunus

Reputation: 214

if employees.isEmpty == false {
    print("This shouldnt be called 1")
    OneSignal.postNotification(["contents": ["en": "\(message)"],
                                "include_player_ids": employees,
                                "headings": ["en": "Hey \(businessName)"],
                                "ios_badgeType": "Increase",
                                "ios_badgeCount": 1])
} else {
    print("Empty no execution")

    print(employees)
    print("Checking inside employees here")
    print(uids)
    print("Checking inside uids here")}

    if uids.isEmpty == false {
        print("This shouldnt be called 2")
        let pathing = ref.child("notification").child(uids).childByAutoId()
        pathing.setValue(values)
    } else {
        print("Empty no execution")
    }

Try the above code. I have moved the part where you show the array in else so that if array is nil it will show.

Upvotes: 1

Related Questions