NIjar NIjar - 3 months ago 15
Swift Question

Required alternative for performSelector in Swift

What would be the replacement for performSelector in Swift? Please suggest below is my code. I need to update the statement I commented in DFURLPrepare class method.

class Login {
func loginRequest(url:String, dictParams: Dictionary <String, String>)
{
let urlPrepare = DFURLPrepare()
urlPrepare.sendRequest(self, url: url, dictParams: dictParams, successMethod: "getDefaultItemsResponse", errorMethod: nil)
}
}

class DFURLPrepare {
func sendRequest (delegate:AnyObject, url : String, dictParams: Dictionary <String, String>,successMethod: String?, errorMethod:String?){
let networkObj = Network()
let requestResource = Resource(url:url,paramdict: dictParams)
networkObj.load(requestResource){ data, response, error in
if let httpResponse = response as? NSHTTPURLResponse {
let statusCode = httpResponse.statusCode
if statusCode == 200 && data != nil{
/// *************Here as in objeactive C **************//
/// [self.delegate performSelector:successMethod withObject:data]];
///// What would be code at place of above statement
}
}
}
}


}

Answer

You can use performSelector(_:withObject:) for NSObject-descendants:

class Login: NSObject { //<-Login needs to be a subclass of `NSObject`.
    func loginRequest(url:String, dictParams: Dictionary <String, String>)
    {
        let urlPrepare = DFURLPrepare()
        urlPrepare.sendRequest(self, url: url, dictParams: dictParams, successMethod: #selector(getDefaultItemsResponse), errorMethod: nil)
    }

    func getDefaultItemsResponse(data: NSData?) {

    }
}

class DFURLPrepare {
    func sendRequest(delegate: NSObject, url : String, dictParams: Dictionary <String, String>,successMethod: Selector, errorMethod: Selector){
        //Type of `delegate` needs to be `NSObject`.
        let networkObj =  Network()
        let requestResource = Resource(url:url,paramdict: dictParams)
        networkObj.load(requestResource){ data, response, error in
            if let httpResponse = response as? NSHTTPURLResponse {
                let statusCode = httpResponse.statusCode
                if statusCode == 200 && data != nil{
                    delegate.performSelector(successMethod, withObject: data)
                }
            }
        }
    }
}

But using closure would be a more preferred way by many Swift programmers:

class Login {
    func loginRequest(url: String, dictParams: Dictionary <String, String>) {
        let urlPrepare = DFURLPrepare()
        urlPrepare.sendRequest(url, dictParams: dictParams, successHandler: getDefaultItemsResponse, errorHandler: nil)
    }

    func getDefaultItemsResponse(data: NSData?) {

    }
}

class DFURLPrepare {
    func sendRequest(url : String,
                     dictParams: [String: String],
                     successHandler: ((NSData?)->Void)?,
                     errorHandler: ((NSError?)->Void)?
    ) {
        let networkObj =  Network()
        let requestResource = Resource(url:url,paramdict: dictParams)
        networkObj.load(requestResource){ data, response, error in
            if let httpResponse = response as? NSHTTPURLResponse {
                let statusCode = httpResponse.statusCode
                if statusCode == 200 && data != nil{
                    successHandler?(data)
                }
            }
        }
    }
}