首页 > 解决方案 > Swift - 2 个空字符串数组

问题描述

试图找出为什么有时会在 swift/Xcode 中发生这种情况。当我检查“employees.isEmpty”时返回正确,因为它是空的。当检查“uids.isEmpty”时,它也返回空,但由于某种原因,它认为有值,然后执行我的 firebase 调用,这当然会使应用程序崩溃,因为数组为 nil。为什么会这样?相同的代码。一样的设置。然而,“employees.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)

            }

标签: iosswift

解决方案


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

试试上面的代码。我已经移动了你在 else 中显示数组的部分,这样如果数组为 nil 就会显示出来。


推荐阅读