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

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


当前回答

目前(Swift 2.1)你可以用3种方式检查它:

使用respondsToSelector由@Erik_at_Digit回答 使用的?@Sulthan回答道 用as?接线员: if let delegateMe = self.delegate as?YourCustomViewController { delegateMe.onSuccess () }

基本上,这取决于你想要达到的目标:

例如,如果你的应用程序逻辑需要执行一些操作,委托没有设置或指向委托没有实现onSuccess()方法(协议方法),那么选项1和3是最好的选择,尽管我会使用选项3,这是Swift的方式。 如果你不想在委派为nil或方法没有实现时做任何事情,那么使用选项2。

其他回答

当我开始将我的旧项目更新到Swift 3.2时,我只需要更改方法

respondsToSelector(selector)

to:

responds(to: selector)

我使用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)")
    }
}

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

斯威夫特3:

协议

@objc protocol SomeDelegate {
    @objc optional func method()
}

对象

class SomeObject : NSObject {

weak var delegate:SomeObject?

func delegateMethod() {

     if let delegateMethod = delegate?.method{
         delegateMethod()
     }else {
        //Failed
     }

   }

}

我只是在一个项目中实现了这一点,见下面的代码。正如@Christopher Pickslay提到的,重要的是要记住函数是第一类公民,因此可以像可选变量一样对待。

@objc protocol ContactDetailsDelegate: class {

    optional func deleteContact(contact: Contact) -> NSError?
}

...

weak var delegate:ContactDetailsDelegate!

if let deleteContact = delegate.deleteContact {
    deleteContact(contact)
}