notice below that cats is of type json returning from the server, and when I use try! cats?.write(toFile: file, atomically: false)
the file contains an xml output while i need to stay as json, any help is much appreciated!
let text = ""
try! text.write(toFile: file, atomically: false, encoding: String.Encoding.utf8)
let parameters : NSMutableDictionary? = [`enter code here`]
let manager = AFHTTPSessionManager()
manager.post("http://appsandgamesinc.com/api/AppAsUser/getMedias/", parameters: parameters, progress: nil, success: { (task: URLSessionTask, responseObject) in
let response = responseObject as? NSDictionary
let medias = response?.object(forKey: "medias") as? NSDictionary
let cats = medias as? NSDictionary
print("cats:\(cats)")
NSKeyedArchiver.archivedData(withRootObject: medias)
try! cats?.write(toFile: file, atomically: false)
print("file://"+file)
})
Type of cats is NSDictionary
if you want to save JSON in file then save it using data. Also with Swift use Swift's native Dictionary and Array
instead of NSDictionary
and NSArray
.
manager.post("http://appsandgamesinc.com/api/AppAsUser/getMedias/", parameters: parameters, progress: nil, success: { (task: URLSessionTask, responseObject) in
if let response = responseObject as? [String:Any],
let medias = response["medias"] as? [String:Any] {
if let data = try? JSONSerialization.data(withJSONObject: medias) {
try? data.write(to: URL(fileURLWithPath: file), options: .atomic)
}
print("cats:\(cats)")
NSKeyedArchiver.archivedData(withRootObject: medias)
print("file://"+file)
}
})