Swift 中 performSelector 的必需替代项
Required alternative for performSelector in Swift
Swift 中 performSelector 的替代品是什么?请在下面建议是我的代码。我需要更新我在 DFURLPrepare class 方法中评论的语句。
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
}
}
}
}
}
您可以将 performSelector(_:withObject:)
用于 NSObject
-后代:
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)
}
}
}
}
}
Swift 中 performSelector 的替代品是什么?请在下面建议是我的代码。我需要更新我在 DFURLPrepare class 方法中评论的语句。
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
}
}
}
}
}
您可以将 performSelector(_:withObject:)
用于 NSObject
-后代:
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)
}
}
}
}
}