Search code examples
iosswiftrestkit

Global handling of 401 Responses with RestKit and Swift


I am currently working on an iOS App that is developed using Swift. For the REST calls, I am using the RestKit framework.

The next stage of my project is to start using authentication against the services. An issue that I have come up against is handling 401 (Not Authenticated) responses from the service. In all of these circumstances, I would like to display a login page. I want to avoid implementing the error handling of this multiple times.

I followed the tutorial at http://blog.higgsboson.tk/2013/09/03/global-request-management-with-restkit/. However, this is in Objective-C and I would like to do things slightly differently.

As such, I want to build a class that extends RKObjectRequestOperation as in the tutorial but using Swift. I have come up with an issue as I am receiving the error

Overriding method with selector 'setCompletionBlockWithSuccess:failure:' has incompatible type '((RKObjectRequestOperation, RKMappingResult) -> Void (RKObjectRequestOperation, NSError) -> Void) -> Void'

I am a bit stuck on this and so was hoping someone could help. The code for the method that is failing is below.

class CustomRequestOperation : RKObjectRequestOperation {
    func setCompletionBlockWithSuccess(success: (operation: RKObjectRequestOperation, mappingResult: RKMappingResult) -> Void, failure: (operation: RKObjectRequestOperation, error: NSError) -> Void) -> Void {

    }
}

Can anyone point out what is wrong with my method signature?


Solution

  • You're overriding the method so you can get Xcode to add the signature for you if you start typing the method name and hie escape.

    It should be

    func setCompletionBlockWithSuccess(success: (operation: RKObjectRequestOperation, mappingResult: RKMappingResult) -> Void, failure: (operation: RKObjectRequestOperation, error: NSError) -> Void) {
    

    (you are adding a return spec that doesn't exist in the superclass method)