Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting mutableCopy to Swift

I've found this guide to receive a HTTP Body, witch contains a JSON formatted error message with AFNetworking 2. The guide is in Objective-C and I'm trying my best too convert it to Swift.

Here's the code I'm trying to convert into 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 specific it's this part there's the 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) {
                    // The question.....

                    var jsonError: NSError
                    // parse to json

                    // Missing some returns with AnyObejct...   
                }

            return nil
        }
    }
}

How do I convert this line into Swift? I'm quite new with the Swift / Objective-C language, so there might be a simple solution for it, but I've not been able to find it yet.

like image 982
Michael Sørensen Avatar asked Apr 07 '15 14:04

Michael Sørensen


2 Answers

I think this should do the trick:

var userInfo = error.userInfo
like image 133
Mladen Prajdic Avatar answered Oct 12 '22 03:10

Mladen Prajdic


I found the same guide that describes how to parse error message in AFNetworking 2 and here is my implementation in Swift:

override func responseObjectForResponse(response: NSURLResponse!, data: NSData!, error: NSErrorPointer) -> AnyObject! {
        if !self.validateResponse(response as! NSHTTPURLResponse, data: data, error: error) {
            if error != nil {
                var userInfo = error.memory!.userInfo!
                var jsonError:NSError?

                let json: AnyObject? = NSJSONSerialization.JSONObjectWithData(data!, options: NSJSONReadingOptions.AllowFragments, error: &jsonError)
                userInfo[JSONResponseSerializerWithDataKey] = json;
                error.memory = NSError(domain: error.memory!.domain, code: error.memory!.code, userInfo: userInfo)

            }
            return nil
        }

        return super.responseObjectForResponse(response, data: data, error: error)
    }

Hope this will help someone.

like image 1
Borbea Avatar answered Oct 12 '22 02:10

Borbea