Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ARTMessage.data is not converting into Dictionary #643

Closed
gouravgupta72 opened this issue Nov 2, 2017 · 8 comments
Closed

ARTMessage.data is not converting into Dictionary #643

gouravgupta72 opened this issue Nov 2, 2017 · 8 comments

Comments

@gouravgupta72
Copy link

gouravgupta72 commented Nov 2, 2017

Hey folks,

Response in ARTMessage.data is coming as Any, when we try to convert it to Dictionary<String, Any> to get all payload information it is giving error.
Can u help us in solving this.

if msg.data != nil {
if let message = msg.data as! Dictionary <String, Any>
{
// Process message
}
}

@tcard
Copy link
Contributor

tcard commented Nov 2, 2017

Hi @gouravgupta72 . What's the error?

@gouravgupta72
Copy link
Author

gouravgupta72 commented Nov 2, 2017

When trying to direct cast it's crashing and when doing above check it is saying
"initializer of conditional binding must have optional type"
screen shot 2017-11-02 at 5 03 19 pm

@tcard
Copy link
Contributor

tcard commented Nov 2, 2017

Does casting to NSDictionary work? We use NSJSONSerialization and I'm not sure the resulting NSDictionary can be bridged to a Dictionary<String, Any>.

@gouravgupta72
Copy link
Author

gouravgupta72 commented Nov 2, 2017

No casting not working,
What would be the solution for getting response in dictionary form ?

I try NSJSONSerialization as well

func convertToDictionary(text: String) -> [String: Any]? {
let data = text.data(using: String.Encoding.utf8, allowLossyConversion: false)!
do {
let json = try JSONSerialization.jsonObject(with: data, options: []) as! [String: AnyObject]
return json
} catch let error as NSError {
print("Failed to load: (error.localizedDescription)")
}
return nil
}

but it's also giving error that data is not in proper format.

@tcard
Copy link
Contributor

tcard commented Nov 2, 2017

OK, then it probably isn't being properly encoded as a JSON object. How are you publishing this? Can you share some code on the publishing end?

@gouravgupta72
Copy link
Author

let messageForServer = ["intentName": "incoming_text",
"payload": [
"text": message
],
"user": [
"type": "customer"
]
] as [String : Any]

    self.channel?.publish(self.clientId, data: messageForServer, clientId: self.clientId) { error in
        guard error == nil else {
            self.signalError(error!)
            return
        }

@tcard
Copy link
Contributor

tcard commented Nov 2, 2017

This works for me:

self.channel.subscribe { (msg) in
    print("MESSAGE \((msg.data as! Dictionary<String, Any>)["hey"])")
}

self.channel.publish(nil, data: ["hey": 123])

Prints as expected:

MESSAGE Optional(123)

So I'm not sure what's going on with yours. Can you maybe print String(describing: msg.data) and type(of: msg.data), see what it tells?

Can you confirm you're using the latest version of the library (1.0.9)?

@funkyboy
Copy link
Contributor

@gouravgupta72 closing this. Feel free to reopen if it's still an issue.
FYI, current version of the SDK is now 1.0.10.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

3 participants