I found this guide to get the HTTP body, because it contains an error message with JSON formatting with AFNetworking 2. The guide is in Objective-C and I try my best to turn it into Swift.
Here is the code I'm trying to convert to Swift:
- (id)responseObjectForResponse:(NSURLResponse *)response data:(NSData *)data error:(NSError *__autoreleasing *)error { if (![self validateResponse:(NSHTTPURLResponse *)response data:data error:error]) { if (*error != nil) { NSMutableDictionary *userInfo = [(*error).userInfo mutableCopy]; NSError *jsonError; // parse to json id json = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingAllowFragments error:&jsonError]; // store the value in userInfo if JSON has no error if (jsonError == nil) userInfo[JSONResponseSerializerWithDataKey] = json; NSError *newError = [NSError errorWithDomain:(*error).domain code:(*error).code userInfo:userInfo]; (*error) = newError; } return (nil); } return ([super responseObjectForResponse:response data:data error:error]); }
More specifically, this part has a problem:
NSMutableDictionary *userInfo = [(*error).userInfo mutableCopy];
This is my current code:
class JSONResponseSerializerWithData: AFJSONResponseSerializer { let JSONResponseSerializerWithDataKey: NSString = "JSONResponseSerializerWithDataKey" override func responseObjectForResponse(response: NSURLResponse!, data: NSData!, error: NSErrorPointer) -> AnyObject? { if(!self.validateResponse(response as NSHTTPURLResponse, data: data, error: error)) { if(error != nil) {
How to convert this string to Swift? I am new to Swift / Objective-C, so there may be a simple solution for it, but I have not been able to find it yet.
source share