如何在swift中根据条件替换方法名?

时间:2016-09-04 02:53:47

标签: ios iphone swift swift2

有没有办法根据swift2.2中的条件用另一个方法名替换方法名?

示例:

 if(self.RecivedString == "varMethod"){
     self.routes.varMethod(postData, andCompletionHandler: { (response, error) in
                if(error.length == 0){
                    if(response.valueForKey("status") as! NSInteger == 1){

                        self.performSelectorOnMainThread(#selector(SubmitViewController.navigateToSubmissionViewController), withObject: nil, waitUntilDone: true)
                    }else{
                        self.showalert(response.valueForKey("msg") as! String)
                    }


                }else{
                    self.showalert(error as String)
                }
            })


  }  

在上面的示例中,我想根据条件将self.routes.varMethod名称替换为其他self.routes.varMethodNew名称。

1 个答案:

答案 0 :(得分:0)

您不能用其他方法名称替换方法名称,但您可以使用闭包变量来保存对方法的引用。

假设您有两个这样的类:

class Routes {
    typealias CompletionHandler = ([String: AnyObject], NSError?)->Void
    func varMethod(data: NSData, andCompletionHandler: CompletionHandler) {
        //...
    }
    func varMethodNew(data: NSData, andCompletionHandler: CompletionHandler) {
        //...
    }
}

class SubmitViewController: UIViewController {
    var routes: Routes = Routes()
    var recievedString: String?
    func showAlert(message: String) {/*...*/}
    func navigateToSubmissionViewController() {/*...*/}
    //...
}

您可以使用SubmitViewController中的闭包变量编写一些方法作为:

func someMethod(postData: NSData) {
    //Declare a closure variable
    let theMethod: (NSData, andCompletionHandler: Routes.CompletionHandler)->Void //<-Make this match to `varMethod` and `varMethodNew`.
    if self.recievedString == "varMethod" {
        theMethod = self.routes.varMethod
    } else {
        theMethod = self.routes.varMethodNew
    }
    //And call it
    theMethod(postData, andCompletionHandler: { response, error in
        if error == nil {
            if response["status"] as! Int == 1 {
                dispatch_async(dispatch_get_main_queue()) {
                    self.navigateToSubmissionViewController()
                }
            } else {
                self.showAlert(response["msg"] as! String)
            }
        } else {
            self.showAlert(error!.localizedDescription)
        }
    })
}

但在您的情况下,您可以将完成处理程序定义为闭包变量:

func someOtherMethod(postData: NSData) {
    //Declare a closure variable
    let completionHandler: Routes.CompletionHandler = { response, error in
        if error == nil {
            if response["status"] as! Int == 1 {
                dispatch_async(dispatch_get_main_queue()) {
                    self.navigateToSubmissionViewController()
                }
            } else {
                self.showAlert(response["msg"] as! String)
            }
        } else {
            self.showAlert(error!.localizedDescription)
        }
    }
    //And use it
    if self.recievedString == "varMethod" {
        self.routes.varMethod(postData, andCompletionHandler: completionHandler)
    } else {
        self.routes.varMethodNew(postData, andCompletionHandler: completionHandler)
    }
}