1

I have the following PubNub delegate methods that are getting called in the exact order as declared below.

public func pubnubClient(client: PubNub!, didSubscribeOnChannels channels: NSArray!) {
    log.info("Subscribed to \(channels.count) channels")
    PubNub.sendMessage("{\"type\": \"CHAT_MSG\",\"msgId\": \"1233123\",\"text\": \"hello\",\"username\": \"attheodo\",\"uuid\": \"user_1\",\"associatedPlaceId\": 2}", toChannel:pubnubChannels[0])
}

public func pubnubClient(client: PubNub!, subscriptionDidFailWithError error: PNError!){
    log.error("Subscribe Error: \(error)")
}

// MARK: Messages
public func pubnubClient(client: PubNub!, willSendMessage message: PNMessage!) {

    let payload = JSON(message.message)
    println(payload)
    println(payload.rawString())
    println(_stdlib_getTypeName(payload))
    println(payload["type"])


}

public func pubnubClient(client: PubNub!, didReceiveMessage message: PNMessage!) {

    let payload = JSON(message.message)
    println(payload)
    println(payload.rawString())
    println(_stdlib_getTypeName(payload))
    println(payload["type"])

}

The problem is SwiftyJSON will not parse properly the string returned by willSendMessage but will parse properly the one returned by didReceiveMessage although, to my eyes, it appears that they are exactly the same. It kinda drives me crazy.

Please check the respective console output below:

// willSendMessage
{"type": "CHAT_MSG","msgId": "1233123","text": "hello","username":     "attheodo","uuid": "user_1","associatedPlaceId": 2}

Optional("{\"type\": \"CHAT_MSG\",\"msgId\": \"1233123\",\"text\":  \"hello\",\"username\": \"attheodo\",\"uuid\": \"user_1\",\"associatedPlaceId\": 2}")

_TtV10SwiftyJSON4JSON

// cannot find payload["type"]
null

// didReceiveMessage
{
  "username" : "attheodo",
  "uuid" : "user_1",
  "msgId" : "1233123",
  "associatedPlaceId" : 2,
  "type" : "CHAT_MSG",
  "text" : "hello"
}

Optional("{\n  \"username\" : \"attheodo\",\n  \"uuid\" : \"user_1\",\n  \"msgId\" : \"1233123\",\n  \"associatedPlaceId\" : 2,\n  \"type\" : \"CHAT_MSG\",\n  \"text\" : \"hello\"\n}")

_TtV10SwiftyJSON4JSON

// payload["type"] is ok
CHAT_MSG

What the @#$! is going on here? Please help, this will put me on meds >:|

4

1 回答 1

0

PubNub 库将为您自动编码/解码 JSON 以用于对象类型,例如字典。尝试排除您对 3rd 方 JSON 库的使用并使用本机 Obj-c 字典。

于 2015-02-06T13:43:47.487 回答