Bool being seen as int when using AnyObject

前端 未结 2 1425
南旧
南旧 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:33

    Instead of:

    var params = Dictionary()
    

    Try:

    var params = Dictionary()
    

    Any can represent an instance of any type at all, including function types.

    Documentation: https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/TypeCasting.html#//apple_ref/doc/uid/TP40014097-CH22-XID_448

    In this case it appears you need a Dictionary and the service is expecting "true" as opposed to the bool value.

    I recommend creating a function to convert your bool value to a String and using that to set your params["tester"].

    Example:

    param["tester"] = strFromBool(true)
    

    and then define the function strFromBool to accept a bool parameter and return "true" or "false" depending on its value.

提交回复
热议问题