I am creating chat app with my backend web-service and all things are going good i have just one issue is that i am getting all messages when i call API but now i want to filter messages to date wise like today is 30th Dec 2020 than i want to show date in tableview header and than messages here is my json response
JSON Response
{
"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" : ""
}
and here is the my function in which i am calling webservice
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)
}
})
}
and here is my model class for parsing json data
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
}
}
now i want to filter message msg_timestamp which i am getting in my json response as i am new to swift i have no idea that how to filter messages by msg_timestamp so any please help me to solve this out
I would recommend to use Codable interface to map the object.
Example:
To map this object in Alamofire you can use responseDecodable method.
Example:
Convert string date to
Dateobject using:And then you will be able to compare that dates. Here you have description of the
Dateobject. So then you can use filter method on that array.