gpt4 book ai didi

ios - 在 Swift 中检查 NSCFBoolean?

转载 作者:行者123 更新时间:2023-11-28 11:49:43 27 4
gpt4 key购买 nike

我正在 iOS 中制作一个待办事项列表应用程序。我将用户的任务存储在 Firebase 中。任务完成后,我更新数据库中的一个值。当读回该值时,由于我之前遇到的一些错误,我测试了它的 Bool、NSNumber 和 String。使用 type(of:),该值被读取为“__NSCFBoolean”,并且我在 switch 语句中的默认情况正在运行。我如何测试这种类型?

抱歉,如果这是一个重复的问题,我只能在 Objective-C 中找到带有 NSCFBoolean 的帖子。

这是我当前的代码:

func observeForChildAdded(tableView: UITableView, snapshot: DataSnapshot) {
let snapshotValue = snapshot.value as! Dictionary<String,Any>
let taskIsCompleted = getIsCompletedSnapshotValue(snapshotValue: snapshotValue)

}

func getIsCompletedSnapshotValue(snapshotValue: Dictionary<String,Any>) -> Bool {
print(“Complete value : \(snapshotValue[“isCompleted”]!) : \(type(of: snapshotValue[“isCompleted”]!))”)

if let isCompValue = snapshotValue[“isCompleted”]! as? Bool {
return isCompValue
} else if let isCompValue = snapshotValue[“isCompleted”]! as? Int {
return (isCompValue == 1)
} else if let isCompValue = snapshotValue[“isCompleted”]! as? String {
return (isCompValue == “true”)
}
return false
}

每当将 child 添加到 Firebase 数据库时,都会调用 observeForChildAdded()。它打印:完整值:0:__NSCFBoolean

最佳答案

这是一个独立示例,演示了 __NSCFBoolean 可以转换为 BoolInt,但不能转换为 String:

let snapshotValue: [String : Any] = ["isCompleted": false as CFBoolean, "isCompleted2": true as CFBoolean]

print("snapshotValue[\"isCompleted\"] is of type \(type(of: snapshotValue["isCompleted"]!))")
print("snapshotValue[\"isCompleted2\"] is of type \(type(of: snapshotValue["isCompleted2"]!))")

if let b1 = snapshotValue["isCompleted"] as? Bool {
print("isCompleted is \(b1)")
} else {
print("isCompleted is not a Bool")
}

if let b2 = snapshotValue["isCompleted2"] as? Bool {
print("isCompleted2 is \(b2)")
} else {
print("isCompleted2 is not a Bool")
}

if let i1 = snapshotValue["isCompleted"] as? Int {
print("isCompleted is \(i1)")
} else {
print("isCompleted is not an Int")
}

if let i2 = snapshotValue["isCompleted2"] as? Int {
print("isCompleted2 is \(i2)")
} else {
print("isCompleted2 is not an Int")
}

if let s1 = snapshotValue["isCompleted"] as? String {
print("isCompleted is \(s1)")
} else {
print("isCompleted is not a String")
}

if let s2 = snapshotValue["isCompleted2"] as? String {
print("isCompleted2 is \(s2)")
} else {
print("isCompleted2 is not a String")
}

// Testing with a switch
switch snapshotValue["isCompleted"] {
case let b as Bool:
print("it is a Bool with value \(b)")
case let i as Int:
print("it is an Int with value \(i)")
case let s as String:
print("it is a String with value \(s)")
default:
print("is is something else")
}

输出

snapshotValue["isCompleted"] is of type __NSCFBoolean
snapshotValue["isCompleted2"] is of type __NSCFBoolean
isCompleted is false
isCompleted2 is true
isCompleted is 0
isCompleted2 is 1
isCompleted is not a String
isCompleted2 is not a String
it is a Bool with value false

关于ios - 在 Swift 中检查 NSCFBoolean?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51972952/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com