Created
September 1, 2016 09:24
-
-
Save juliengdt/c72b064a57e13f0bb89e2f6141d08ecb to your computer and use it in GitHub Desktop.
Pretty print extension for Alamofire Request
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
extension Alamofire.Request { | |
func responseDebugPrint() -> Self { | |
return responseJSON() { | |
response in | |
if let JSON: AnyObject = response.result.value, | |
JSONData = try? NSJSONSerialization.dataWithJSONObject(JSON, options: .PrettyPrinted), | |
prettyString = NSString(data: JSONData, encoding: NSUTF8StringEncoding) { | |
print(prettyString) | |
} else if let error = response.result.error { | |
print("Error Debug Print: \(error.localizedDescription)") | |
} | |
} | |
} | |
} |
extension DataRequest {
@discardableResult
func prettyPrintedJsonResponse() -> Self {
return responseJSON { (response) in
switch response.result {
case .success(let result):
if let data = try? JSONSerialization.data(withJSONObject: result, options: .prettyPrinted),
let text = String(data: data, encoding: .utf8) {
print("📗 prettyPrinted JSON response: \n \(text)")
}
case .failure: break
}
}
}
}
@smaljaar How to use it?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Dude, it's giving me error on line 3 ,
use of unresolved identifier "responseJSON"