ios - 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)
}
解决方案
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 就会显示出来。
推荐阅读
- java - 使用 Stream API 按权限对用户进行分组
- node.js - 从节点应用程序发布到 Wordpress 博客的最佳方式是什么?
- java - 未找到 ID 为“org.springframework.boot”的插件
- mongodb - 如何使用 Mongo Stitch React-Native SDK 获取 Google OAuth 的身份验证代码?
- csv - 无法使用 fortran 打开 csv
- r - abline 不在 R 中绘图
- node.js - jwt token validation on main page
- amazon-web-services - 如何从 S3 下载数百个机密文件?
- php - 无效的 JSON 有效负载 Google Sheets API 附加字段
- c# - WPF - 防止鼠标滚轮在 ComboBox 中滚动并滚动包含 Listview