ios - JSQMessageData 追加 Json 数据
<p><p>我正在尝试在 JSQMessageData 中添加 json 值以在 JSQMessagesViewController 上显示消息。 View 已设置,这是 lite 聊天(只能聊天一次)。我们使用 api 来 <code>send</code> 和 <code>receive</code> 消息。问题是当我从 api 作为 <code>json</code> 获取数据时,它返回值。我想将该 <code>json</code> 数据附加到我的 <code>JSQMessages</code> 对象的其余部分,过去几天我尝试过但未能完成此操作。这是完整的代码和 <code>json</code> 响应。 </p>
<pre><code> APIHandler.requestGETURL(urlString, success: { (JSON) in
print(JSON)
// var messageDictionary : = []
// this is the message object
// i want to add the json data to my messageDictionary
// reload collection view
/*
{
"message_time" : "27-05-2017",
"user_id" : 1924,
"user_name" : "Tester name",
"message" : "hi",
"user_thumb" : "<image_path>"
},
{
"message_time" : "27-05-2017",
"user_id" : 1924,
"user_name" : "Tester name",
"message" : "how are you?",
"user_thumb" : "<image_path>"
}
*/
// i want to
let arrayNames =JSON["data"]
self.messageDictionary.append(JSQMessageData())
// I am stuck here
}) { (Error) in
print(Error.localizedDescription)
}
</code></pre></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>如果我理解正确,您正在尝试将 <code>json</code> 解析为 <code>JSQMessage</code> 对象。您的消息数据并不过分复杂,它包含标准 <code>JSQMessage</code> 所需的所有内容。所以没有任何理由创建自己的 <code>JSQMessageData</code> 对象。您可以只使用 <code>JSQMessage</code> 初始化程序之一。由于您只使用“文本”消息而不是任何其他“媒体”</p>
<pre><code>JSQMessage(senderId: <String!>, senderDisplayName: <String!>, date: <#Date>, text: <String>)
</code></pre>
<p>应该是你所需要的。所以你需要做的就是从你的 json 响应中获取值。有很多方法可以做到这一点。</p>
<p>我将假设您提供的 <code>json</code> 也包含在这样的列表中</p>
<pre><code>[
{ "message_time" : "27-05-2017",
"user_id" : 1924,
"user_name" : "Tester name",
"message" : "hi",
"user_thumb" : "<image_path>"
},
{ "message_time" : "27-05-2017",
"user_id" : 1924,
"user_name" : "Tester name",
"message" : "how are you?",
"user_thumb" : "<image_path>"
}
]
</code></pre>
<p>我们可以利用 <code>flatmap</code> 函数从 <code>json</code> 数据中获取我们的“消息”。您也不需要字典,因为每条消息都没有键,所以只需使用包含 <code>JSQMessageObject</code>s</p> 的列表
<pre><code>var messages: = []
var imageDictionary: = [:]
APIHandler.requestGETURL(urlString, success: { (JSON) in
print(JSON)
let messagesJSON = response.result.value as? [] ?? [[:]]
guard let listOfMessages = JSON as? []
messages: = listOfMessages.flatmap { messageData in
guard let dateCreated = messageData["message_time"] as? Date,
let userID = messageData["user_id"] as? String,
let userName = messageData["user_name"] as? String,
let text = messageData["message"] as? Stringelse {
//If any of these things are missing we do not want to save the entry
return nil
}
let imagePath = messageData["user_thumb"] as? String
</code></pre>
<p>imageDictionary = imagePath
return JSQMessage(senderId: userID, senderDisplayName: userName, date: dateCreated, text: text)</p>
<pre><code>}) { (let error: Error) in
if error != nil {
print(Error.localizedDescription)
}
}
</code></pre>
<p>我会将您的图像路径保存到字典中并在后台线程中获取它们,这样用户就可以在图像到达时查看消息。然后,一旦他们加载了它们,就将它们应用到您的消息中。或者您可以将其添加到您自己的符合 <code>JSQMessageDataSource</code> 协议(protocol)的自定义消息对象中。有关如何完成该任务的更多信息,请查看 <a href="https://stackoverflow.com/questions/38864942/argument-labels-do-not-match-any-available-overloads/38884743#38884743" rel="noreferrer noopener nofollow">this post</a> </p></p>
<p style="font-size: 20px;">关于ios - JSQMessageData 追加 Json 数据,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/44713685/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/44713685/
</a>
</p>
页:
[1]