Swift - 2 пустых массива строк - PullRequest
0 голосов
/ 18 апреля 2019

Попытка выяснить, почему иногда это происходит в swift / Xcode.Когда я проверяю "employee.isEmpty", возвращается правильно, потому что он пуст.Когда проверяется «uids.isEmpty», он также возвращает пустое значение, но по какой-то причине он думает, что есть значения, а затем выполняет мой вызов firebase, который, конечно, завершает работу приложения, поскольку массив равен нулю.Почему это происходит?Тот же кодТо же самое.И все же «employee.isEmpty» возвращает nil, как и должно быть (в моем тестировании), а другой «uids.isEmpty» пуст, но выполняется так, как если есть значения, а их нет.

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")
}

Они оба печатают одинаково, когда не назначено никаких значений, например: «[]».

Декларации / Приложение

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)

            }

1 Ответ

1 голос
/ 18 апреля 2019
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")
    }

Попробуйте приведенный выше код. Я переместил часть, где вы показываете массив в другое, так что если массив равен нулю, он покажет.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...