【问题标题】:how to replace a method name based on condition in swift?如何根据条件快速替换方法名称?
【发布时间】:2016-09-04 02:53:46
【问题描述】:

有没有什么方法可以根据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 名称。

【问题讨论】:

  • 我不认为您可以替换一种方法,但是为了实现您的目标,您可以创建一种新方法(即 varMethodNew)并根据您的要求并根据某些条件调用所需的方法。

标签: ios iphone swift swift2


【解决方案1】:

你不能用另一个方法名替换一个方法名,但是你可以有一个闭包变量来保存对方法的引用。

假设你有两个这样的类:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-05-08
    • 1970-01-01
    • 1970-01-01
    • 2023-02-15
    • 2017-02-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多