Bool being seen as int when using AnyObject

10,173

Solution 1

Instead of:

var params = Dictionary<String,AnyObject>()

Try:

var params = Dictionary<String,Any>()

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.

Solution 2

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<String,AnyObject>()
params["tester"] = true

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

That's the safe way to do it.

Share:
10,173

Related videos on Youtube

steventnorris
Author by

steventnorris

Applications developer with a preference for object-oriented languages. Currently working at FortyAU, a development firm in Nashville, TN. http://www.fortyau.com

Updated on September 14, 2022

Comments

  • steventnorris
    steventnorris over 1 year

    I am using a list of params of type Dictionary<String,AnyObject> in a Swift iOS application to hold some parameters that will eventually be passed to a webservice. If I were to save a Bool to this dictionary and then print it, it appears as "Optional(1)", so it is being converted to an int. I cannot send an int and need this to be "true". Sample code below.

    var params = Dictionary<String,AnyObject>()
    params["tester"] = true
    println(params["tester"])
    

    How can I get this to save as an actual true/false value and not as an integer?

    Caveats

    This is being used with AFNetworking, so the parameters are required to be of the form AnyObject!

    The AFNetworking structure is as below:

    manager.GET(<#URLString: String!#>, parameters: <#AnyObject!#>, success: <#((AFHTTPRequestOperation!, AnyObject!) -> Void)!##(AFHTTPRequestOperation!, AnyObject!) -> Void#>, failure: <#((AFHTTPRequestOperation!, NSError!) -> Void)!##(AFHTTPRequestOperation!, NSError!) -> Void#>)
    

    the 'parameters' argument is what I am passing in the Dictionary<String,AnyObject> as.

  • steventnorris
    steventnorris about 9 years
    So, I'm using AFNetworking, which the methods for GET, POST, DELETE, etc require parameters in a form of 'AnyObject!', which I didn't realize. This limits me to using that data type, which Any does not conform to.
  • steventnorris
    steventnorris about 9 years
    The issue is that it's hitting the service as {"tester":1} not {"tester":true}, which is not accepted.
  • steventnorris
    steventnorris about 9 years
    For reference, when trying to use the AFNetworking methods, I get the 'type Dictionary<String,Any> does not conform to AnyObject' error.
  • gregheo
    gregheo about 9 years
    Ah, I misunderstood. What about wrapping the value directly into an NSNumber – params["tester"] = NSNumber(bool: true)
  • steventnorris
    steventnorris about 9 years
    I don't want a number. I want it to save into the param array and send as true/false. I'm adding some above code to further clarify.
  • chrissukhram
    chrissukhram about 9 years
    Could you just store the result as String and decide which string to input into the Dictionary based on the bool value? i.e param["tester"] = "true" since it seems you do not need the bool locally but to send it to the service.
  • gregheo
    gregheo about 9 years
    I know, but you need some way to signal to AFNetworking that you want the thing to be a Boolean. Boxing up values in NSNumber objects is one way to do that.
  • chrissukhram
    chrissukhram about 9 years
    You could also store it locally but just have a method to convert the bool to a string. param["tester"] = strFromBool(true). This way u can pass it a variable if needed
  • steventnorris
    steventnorris about 9 years
    That seems to work fine. While it's not ideal (it means there are some use case caveats. I'm packaging this to be used across multiple apps.), it does work. I'm not sure I see another work around though.
  • chrissukhram
    chrissukhram about 9 years
    I have updated with a way to make it a bit cleaner. Sorry im on mobile at the momebt
  • steventnorris
    steventnorris about 9 years
    This still sends as a number, not as true/false.
  • steventnorris
    steventnorris about 9 years
    It looks as though, in my case, I need to send the bool values as strings for them to come across as true/false rather than int conversions. My assumption is that the webservices uses string values over raw primitives, otherwise the 0/1 should convert fine. Given that, please add the string solution as a second option on your answer and I will accept.
  • Martin R
    Martin R about 9 years
    Since this answer has been accepted, it would be useful to future readers to update it to the actual solution (which is only mentioned in the comments up to now).