What are the problems encountered in the encapsulation of swift json to model, generalization?

   
 //let rootModel = Mapper<RootClass>().map(JSONString: jsonString);  

static func modelWithJSONFile(fileName:String) -> AnyObject? {

    let jsonString = fileName.jsonFileToJSONString();
    let rootModel = Mapper<self.classForCoder()>().map(JSONString: jsonString);
    return rootModel;
}


func jsonFileToJSONString() -> String {
    assert(self.contains(".geojson") == true);
    
    if self.contains(".geojson") == true {
        let array: Array = self.components(separatedBy: ".");
        let path = Bundle.main.path(forResource: array.first, ofType: array.last);
        if let jsonData = try? Data(contentsOf: URL(fileURLWithPath: path!)) {
            
            if let jsonObj = try? JSONSerialization.jsonObject(with: jsonData, options: JSONSerialization.ReadingOptions(rawValue: 0)) {
                let jsonString = ((jsonObj as! NSDictionary).JSONValue()!).removingPercentEncoding!;
                print(jsonString);
                return jsonString;
            }
            return "";
        }
        return "";
    }
    return "";
}



May.25,2021
Menu