Bool being seen as int when using AnyObject

前端 未结 2 1419
南旧
南旧 2020-12-16 21:41

I am using a list of params of type Dictionary in a Swift iOS application to hold some parameters that will eventually be passed to a we

2条回答
  •  时光取名叫无心
    2020-12-16 22:29

    true really is 1, so it's not inaccurate; it really is storing the Bool, but since it's coming out the other end as AnyObject, it's just printing it out as an integer since it doesn't know the exact type.

    You can try to cast it to test for a Bool:

    var params = Dictionary()
    params["tester"] = true
    
    if let boolValue = params["tester"] as? Bool {
      println("\(boolValue)")
    }
    

    That's the safe way to do it.

提交回复
热议问题