1
我有以下PubNub委託方法按照下面聲明的確切順序進行調用。SwiftyJSON不會解析JSON字符串,稍後將解析而不會出現問題
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"])
}
的問題是SwiftyJSON將無法正確解析由willSendMessage
返回的字符串,但儘管我的眼睛,看來他們是完全一樣的將正確地解析由didReceiveMessage
返回的一個。它有點讓我發瘋。
請檢查各自的控制檯輸出如下:
// 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
什麼@#$!在這裏?請幫助,這將使我的藥>:|