Thursday, August 11, 2022
HomeiOS Developmentios - Swift JSON type by date

ios – Swift JSON type by date


The json file comprises date data. I need to type objects from latest to oldest. How can I do it utilizing date data. Date data is included as a string. My code is beneath

TH14.json

[{
    "layout_id" : "0",
    "townhall" : "",
    "tags" : "",
    "image" : "",
    "dateString" : "2022-07-12 17:45:42"
},
{
    "layout_id" : "1",
    "townhall" : "",
    "tags" : "",
    "image" : "",
    "dateString" : "2022-07-12 18:45:42"
}]

MapsData.swift

import Basis

struct MapsData: Codable {
    
    var layout_id: String = ""
    var townhall: String = ""
    var tags: String = ""
    var picture: String = ""
    var dateString: String
    
    init(layout_id: String, townhall: String, tags: String, picture: String, dateString: String) {
        
        self.layout_id = layout_id
        self.townhall = townhall
        self.tags = tags
        self.picture = picture
        self.dateString = dateString
        
    }
    
}

parse json

func load(getTH: String, getTag: String) -> String {
    if let  fileLocation = Bundle.most important.url(forResource: getTH, withExtension: "json") {
        do {
            let information = strive Knowledge(contentsOf: fileLocation)
            let jsonDecoder = JSONDecoder()
            let dataFromJson = strive jsonDecoder.decode([MapsData].self, from: information)
            
            if getTH == "empty" {
                self.mapsData = dataFromJson
            }else{
                if getTag == "All" {
                    self.mapsData = dataFromJson
                }else{
                    self.mapsData = dataFromJson
                    self.mapsData = self.mapsData.filter({$0.tags == getTag})
                }
            }
            
            /* self.mapsData.shuffle() */
            print(self.mapsData.rely)
            
        }catch {
            print(error)
        }
    }
    
    return getTag
}

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments