【发布时间】:2026-02-13 21:50:02
【问题描述】:
我正在使用我的后端 Web 服务创建聊天应用程序,一切进展顺利我只有一个问题是,当我致电 API 时,我收到了所有消息,但现在我想像今天一样明智地过滤消息是 2020 年 12 月 30 日,而不是我想在 tableview 标题中显示日期,而这里的消息是我的 json 响应
JSON 响应
{
"status" : true,
"data" : [
{
"gig_status" : 1,
"photo" : "http:\/\/vebbuilders.co.uk\/ugigs_app\/public\/uploads\/users\/34-1609139138.png",
"name" : "Artist",
"image_url" : "",
"message" : null,
"from_me" : 1,
"id" : 34,
"gig_date" : "25 Dec 2020",
"message_id" : 95,
"msg_timestamp" : "2020-12-24T12:58:58.000000Z",
"type" : 2,
"star_rating" : null,
"gig_id" : 43,
"gig_notes" : "Test"
},
{
"gig_status" : 1,
"photo" : "http:\/\/vebbuilders.co.uk\/ugigs_app\/public\/uploads\/users\/34-1609139138.png",
"name" : "Artist",
"image_url" : "",
"message" : null,
"from_me" : 1,
"id" : 34,
"gig_date" : "28 Dec 2020",
"message_id" : 117,
"msg_timestamp" : "2020-12-28T04:45:04.000000Z",
"type" : 2,
"star_rating" : null,
"gig_id" : 50,
"gig_notes" : "Test"
},
{
"gig_status" : 2,
"photo" : "http:\/\/vebbuilders.co.uk\/ugigs_app\/public\/uploads\/users\/34-1609139138.png",
"name" : "Artist",
"image_url" : "",
"message" : null,
"from_me" : 1,
"id" : 34,
"gig_date" : "31 Dec 2020",
"message_id" : 118,
"msg_timestamp" : "2020-12-28T05:04:19.000000Z",
"type" : 2,
"star_rating" : null,
"gig_id" : 62,
"gig_notes" : "new ro ak"
},
{
"gig_status" : 1,
"photo" : "http:\/\/vebbuilders.co.uk\/ugigs_app\/public\/uploads\/users\/34-1609139138.png",
"name" : "Artist",
"image_url" : "",
"message" : null,
"from_me" : 1,
"id" : 34,
"gig_date" : "31 Dec 2020",
"message_id" : 119,
"msg_timestamp" : "2020-12-28T05:11:01.000000Z",
"type" : 2,
"star_rating" : null,
"gig_id" : 62,
"gig_notes" : "new ro ak"
},
{
"gig_status" : 2,
"photo" : "http:\/\/vebbuilders.co.uk\/ugigs_app\/public\/uploads\/users\/34-1609139138.png",
"name" : "Artist",
"image_url" : "",
"message" : null,
"from_me" : 1,
"id" : 34,
"gig_date" : "02 Jan 2021",
"message_id" : 120,
"msg_timestamp" : "2020-12-28T05:13:45.000000Z",
"type" : 2,
"star_rating" : null,
"gig_id" : 70,
"gig_notes" : "Test"
},
{
"gig_status" : 2,
"photo" : "http:\/\/vebbuilders.co.uk\/ugigs_app\/public\/uploads\/users\/34-1609139138.png",
"name" : "Artist",
"image_url" : "",
"message" : null,
"from_me" : 1,
"id" : 34,
"gig_date" : "02 Jan 2021",
"message_id" : 121,
"msg_timestamp" : "2020-12-28T05:15:20.000000Z",
"type" : 2,
"star_rating" : null,
"gig_id" : 70,
"gig_notes" : "Test"
},
{
"gig_status" : 2,
"photo" : "http:\/\/vebbuilders.co.uk\/ugigs_app\/public\/uploads\/users\/34-1609139138.png",
"name" : "Artist",
"image_url" : "",
"message" : null,
"from_me" : 1,
"id" : 34,
"gig_date" : "02 Jan 2021",
"message_id" : 122,
"msg_timestamp" : "2020-12-28T05:23:12.000000Z",
"type" : 2,
"star_rating" : null,
"gig_id" : 70,
"gig_notes" : "Test"
},
{
"gig_status" : 2,
"photo" : "http:\/\/vebbuilders.co.uk\/ugigs_app\/public\/uploads\/users\/34-1609139138.png",
"name" : "Artist",
"image_url" : "",
"message" : null,
"from_me" : 1,
"id" : 34,
"gig_date" : "02 Jan 2021",
"message_id" : 123,
"msg_timestamp" : "2020-12-28T05:26:25.000000Z",
"type" : 2,
"star_rating" : null,
"gig_id" : 70,
"gig_notes" : "Test"
},
{
"type" : 2,
"photo" : "http:\/\/vebbuilders.co.uk\/ugigs_app\/public\/uploads\/users\/34-1609139138.png",
"message_id" : 124,
"star_rating" : null,
"gig_id" : 70,
"name" : "Artist",
"id" : 34,
"gig_date" : "02 Jan 2021",
"gig_notes" : "Test",
"message" : null,
"msg_timestamp" : "2020-12-28T05:32:07.000000Z",
"image_url" : "",
"gig_status" : 2,
"from_me" : 1
},
{
"type" : "3",
"photo" : "http:\/\/vebbuilders.co.uk\/ugigs_app\/public\/uploads\/users\/34-1609139138.png",
"message_id" : 125,
"star_rating" : null,
"gig_id" : null,
"name" : "Artist",
"id" : 34,
"gig_date" : "01 Jan 1970",
"gig_notes" : null,
"message" : null,
"msg_timestamp" : "2020-12-28T06:02:42.000000Z",
"image_url" : "http:\/\/vebbuilders.co.uk\/ugigs_app\/public\/uploads\/users\/34-1609135362.png",
"gig_status" : null,
"from_me" : 0
},
{
"type" : "3",
"photo" : "http:\/\/vebbuilders.co.uk\/ugigs_app\/public\/uploads\/users\/34-1609139138.png",
"message_id" : 126,
"star_rating" : null,
"gig_id" : null,
"name" : "Artist",
"id" : 34,
"gig_date" : "01 Jan 1970",
"gig_notes" : null,
"message" : null,
"msg_timestamp" : "2020-12-28T06:21:48.000000Z",
"image_url" : "http:\/\/vebbuilders.co.uk\/ugigs_app\/public\/uploads\/users\/34-1609136508.png",
"gig_status" : null,
"from_me" : 0
},
{
"type" : 2,
"photo" : "http:\/\/vebbuilders.co.uk\/ugigs_app\/public\/uploads\/users\/34-1609139138.png",
"message_id" : 127,
"star_rating" : null,
"gig_id" : 70,
"name" : "Artist",
"id" : 34,
"gig_date" : "02 Jan 2021",
"gig_notes" : "Test",
"message" : null,
"msg_timestamp" : "2020-12-28T07:32:50.000000Z",
"image_url" : "",
"gig_status" : 1,
"from_me" : 1
},
{
"type" : 2,
"photo" : "http:\/\/vebbuilders.co.uk\/ugigs_app\/public\/uploads\/users\/34-1609139138.png",
"message_id" : 128,
"star_rating" : null,
"gig_id" : 73,
"name" : "Artist",
"id" : 34,
"gig_date" : "10 Jan 2021",
"gig_notes" : "Test",
"message" : null,
"msg_timestamp" : "2020-12-28T10:25:51.000000Z",
"image_url" : "",
"gig_status" : 1,
"from_me" : 1
},
{
"type" : 2,
"photo" : "http:\/\/vebbuilders.co.uk\/ugigs_app\/public\/uploads\/users\/34-1609139138.png",
"message_id" : 129,
"star_rating" : null,
"gig_id" : 74,
"name" : "Artist",
"id" : 34,
"gig_date" : "19 May 2021",
"gig_notes" : "Tehsgsbs",
"message" : null,
"msg_timestamp" : "2020-12-28T10:28:14.000000Z",
"image_url" : "",
"gig_status" : 1,
"from_me" : 1
},
{
"type" : 0,
"photo" : "http:\/\/vebbuilders.co.uk\/ugigs_app\/public\/uploads\/users\/34-1609139138.png",
"message_id" : 136,
"star_rating" : null,
"gig_id" : null,
"name" : "Artist",
"id" : 34,
"gig_date" : "01 Jan 1970",
"gig_notes" : null,
"message" : "test",
"msg_timestamp" : "2020-12-29T08:36:37.000000Z",
"image_url" : "",
"gig_status" : null,
"from_me" : 0
},
{
"type" : 0,
"photo" : "http:\/\/vebbuilders.co.uk\/ugigs_app\/public\/uploads\/users\/34-1609139138.png",
"message_id" : 139,
"star_rating" : null,
"gig_id" : null,
"name" : "Artist",
"id" : 34,
"gig_date" : "01 Jan 1970",
"gig_notes" : null,
"message" : "ccgg",
"msg_timestamp" : "2020-12-30T05:09:13.000000Z",
"image_url" : "",
"gig_status" : null,
"from_me" : 0
},
{
"type" : 0,
"photo" : "http:\/\/vebbuilders.co.uk\/ugigs_app\/public\/uploads\/users\/34-1609139138.png",
"message_id" : 140,
"star_rating" : null,
"gig_id" : null,
"name" : "Artist",
"id" : 34,
"gig_date" : "01 Jan 1970",
"gig_notes" : null,
"message" : "Hello",
"msg_timestamp" : "2020-12-30T06:56:41.000000Z",
"image_url" : "",
"gig_status" : null,
"from_me" : 0
}
],
"message" : ""
}
这是我调用 web 服务的函数
func calllMessageAPI(){
showLoader(title: "Please Wait...", controller: self)
let params = ["id": self.id] as [String : Any]
guard let token = UserDefaults.standard.string(forKey: "token") else { return }
let headers: HTTPHeaders = ["Authorization": "Bearer \(token)"]
print(headers)
AF.request(chatdetail, method: .post, parameters: params, headers: headers).responseJSON(completionHandler: {(response) in
switch response.result{
case.success(let value):
let json = JSON(value)
print(json)
let data = json["data"]
if data.isEmpty{
}else{
self.messageData.removeAll()
data.array?.forEach({ (mList) in
let m_list = NewMessageModel(message: mList["message"].stringValue, type: mList["type"].stringValue, star_rating: mList["star_rating"].doubleValue, gig_notes: mList["gig_notes"].stringValue, gig_status: mList["gig_status"].stringValue, name: mList["name"].stringValue, from_me: mList["from_me"].stringValue, message_id: mList["message_id"].stringValue, msg_timestamp: mList["msg_timestamp"].stringValue, gig_id: mList["gig_id"].stringValue, id: mList["id"].stringValue, image_url: mList["image_url"].stringValue, gig_date: mList["gig_date"].stringValue, photo: mList["photo"].stringValue)
self.messageData.insert(m_list, at: 0)
})
let groupedByLength = Dictionary(grouping: data) {
dayMonthYearDateDescription(date: parseDate(value: (($0["msg_timestamp"] as? String)!))) //Getting error here
}
}
self.tblListView.reloadData()
hideLoader(controller: self)
case.failure(let error):
print(error.localizedDescription)
hideLoader(controller: self)
}
})
}
这是我解析 json 数据的模型类
class NewMessageModel: NSObject{
var message: String?
var type: String?
var star_rating: Double?
var gig_notes: String?
var gig_status: String?
var name: String?
var from_me: String?
var message_id: String?
var msg_timestamp: String?
var gig_id: String?
var id: String?
var image_url: String?
var gig_date: String?
var photo: String?
init(message: String,type: String,star_rating: Double,gig_notes: String,gig_status: String,name: String,from_me: String,message_id: String,msg_timestamp: String,gig_id: String,id: String,image_url: String,gig_date: String,photo: String) {
self.message = message
self.type = type
self.star_rating = star_rating
self.gig_notes = gig_notes
self.gig_status = gig_status
self.name = name
self.from_me = from_me
self.message_id = message_id
self.msg_timestamp = msg_timestamp
self.gig_id = gig_id
self.id = id
self.image_url = image_url
self.gig_date = gig_date
self.photo = photo
}
}
现在我想过滤消息 msg_timestamp,因为我是 swift 的新手,所以我在我的 json 响应中得到了过滤,我不知道如何通过 msg_timestamp 过滤消息,所以请帮我解决这个问题
【问题讨论】:
-
有人帮我,我被卡住了
-
提示:
DateFormatter. -
@ElTomato 谢谢你的提示,但你能告诉我更多关于这个的信息,因为我是 swift 新手
标签: ios swift tableview tableheader