【问题标题】:Check for NSCFBoolean in Swift?在 Swift 中检查 NSCFBoolean?
【发布时间】:2019-01-29 01:47:43
【问题描述】:

我正在 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
}

observeForChildAdded() 会在将孩子添加到 Firebase 数据库时调用。 它打印: 完整值:0:__NSCFBoolean

【问题讨论】:

  • “我测试它的布尔值、NSNumber 和字符串”——你如何测试它?编辑您的问题以包含代码。
  • type(of:) 非常objective-c-ish。我想有更好的原生方式。
  • __NSCFBoolean 应该始终可以转换为NSNumber(因为__NSCFBooleanNSNumber 的私有子类)以及Bool(由于桥接)。你能提供一个minimal reproducible example你所面临的问题吗?
  • 您的代码似乎按我们所拥有的很少的预期工作。要重复 Hamish 的请求,您需要一个最小的、完整的示例来获得有用的答案。
  • @WillTaylor PS:由于您显然对数据库负责,因此您应该保存一致的数据以避免那种不必要的类型舞蹈。

标签: ios swift xcode boolean


【解决方案1】:

这是一个独立的示例,演示 __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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-02-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-30
    • 2016-05-08
    相关资源
    最近更新 更多