Search code examples
iosswiftswift3floating-pointanyobject

Float value returns with double quotes from [String: AnyObject] in swift


I have no idea why the float value always comes with double quotes when I used [String: AnyObject]. Do you guys have some methods so that I can get the correct result?

let appliedLoyalty: Float = 1.05
let appliedWallet: Float = 0.55

let payLoad: [String: AnyObject] = ["custid": custid! as AnyObject, "discounts": ["loyalty": appliedLoyalty,"wallet": appliedWallet] as AnyObject] // custid is string value

print(payLoad)

When I print the payLoad, the float value comes in double quotes.

["discounts": {
    "loyalty" = "1.05";
    "wallet" = "0.45";
}, "custid": "puma"]

Solution

  • Replace AnyObject with Any like below:

    let appliedLoyalty: Float = 1.05
    let appliedWallet: Float = 0.55
    let custID = "puma"
    let payLoad: [String: Any] = ["custid": custID, "discounts": ["loyalty": appliedLoyalty,"wallet": appliedWallet]] // custid is string value
    
    print(payLoad)
    

    enter image description here