我在谷歌上搜索了一下,但没有找到与respondsToSelector:等价的swift:是什么。

这是我唯一能找到的(Swift替代respondsToSelector),但在我的情况下并不太相关,因为它检查委托的存在,我没有委托,我只是想检查在设备上运行时是否存在新的API,如果没有回落到API的以前版本。


当前回答

2017年3月20日更新Swift 3语法:

如果你不关心可选方法是否存在,只需调用delegate?.optionalMethod?()

否则,使用guard可能是最好的方法:

weak var delegate: SomeDelegateWithOptionals?

func someMethod() {
    guard let method = delegate?.optionalMethod else {
        // optional not implemented
        alternativeMethod()
        return
    }
    method()
}

最初的回答:

你可以使用“if let”方法来测试一个可选协议,就像这样:

weak var delegate: SomeDelegateWithOptionals?

func someMethod() {
  if let delegate = delegate {
    if let theMethod = delegate.theOptionalProtocolMethod? {
      theMethod()
      return
    }
  }
  // Reaching here means the delegate doesn't exist or doesn't respond to the optional method
  alternativeMethod()
}

其他回答

swift..另一种可能的语法。

 if let delegate = self.delegate, method = delegate.somemethod{
        method()
    }

我使用guard let else,如果委托func没有实现,它可以做一些默认的事情。

@objc protocol ViewController2Delegate: NSObjectProtocol {

    optional func viewController2(controller: ViewController2, didSomethingWithStringAndReturnVoid string: String)

    optional func viewController2(controller: ViewController2, didSomethingWithStringAndReturnString string: String) -> String
}

class ViewController2: UIViewController {

    weak var delegate: ViewController2Delegate?        

    @IBAction func onVoidButtonClicked(sender: AnyObject){

        if (delegate != nil && delegate!.respondsToSelector(Selector("viewController2:didSomethingWithStringAndReturnVoid:"))) {
            NSLog("ReturnVoid is implemented")

            delegate!.viewController2!(self, didSomethingWithStringAndReturnVoid: "dummy")
        }
        else{
            NSLog("ReturnVoid is not implemented")
            // Do something by default
        }
    }

    @IBAction func onStringButtonClicked(sender: AnyObject){

        guard let result = delegate?.viewController2?(self, didSomethingWithStringAndReturnString: "dummy") else {
            NSLog("ReturnString is not implemented")
            // Do something by default
            return
        }

        NSLog("ReturnString is implemented with result: \(result)")
    }
}

我猜你想为委托做一个默认实现。你可以这样做:

let defaultHandler = {}
(delegate?.method ?? defaultHandler)()

如果你要测试的方法在@objc协议中被定义为可选方法(听起来像你的情况),那么使用可选的链接模式为:

if let result = object.method?(args) {
  /* method exists, result assigned, use result */
}
else { ... }

当方法被声明为返回Void时,只需使用:

if object.method?(args) { ... }

See:

通过可选链接调用方法 摘自:苹果公司《快速编程语言》。 iBooks。https://itun.es/us/jEUH0.l

看起来你需要把你的协议定义为NSObjectProtocol的子协议…然后你会得到respondsToSelector方法

@objc protocol YourDelegate : NSObjectProtocol
{
    func yourDelegateMethod(passObject: SomeObject)
}

注意,只指定@objc是不够的。你还应该注意,实际的委托是NSObject的子类——在Swift中可能不是。