Требуемая альтернатива для executeSelector в Swift

Какой будет замена для executeSelector в Swift? Пожалуйста, предложите ниже мой код. Мне нужно обновить утверждение, которое я прокомментировал в методе класса DFURLPrepare.

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
          }
       }
   }
}

}

1 ответ

Решение

Ты можешь использовать performSelector(_:withObject:) за 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)
                }
            }
        }
    }
}

Но использование замыкания было бы более предпочтительным способом для многих программистов Swift:

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)
                }
            }
        }
    }
}
Другие вопросы по тегам