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

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


当前回答

斯威夫特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
     }

   }

}

其他回答

如果你要测试的方法在@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

对于swift 3.0

import UIKit

@objc protocol ADelegate : NSObjectProtocol {

    @objc optional func hi1()
    @objc optional func hi2(message1:String, message2:String)
}

class SomeObject : NSObject {

    weak var delegate:ADelegate?

    func run() {

        // single method
        if let methodHi1 = delegate?.hi1 {
            methodHi1()
        } else {
            print("fail h1")
        }

        // multiple parameters
        if let methodHi2 = delegate?.hi2 {
            methodHi2("superman", "batman")
        } else {
            print("fail h2")
        }
    }
}

class ViewController: UIViewController, ADelegate {

    let someObject = SomeObject()

    override func viewDidLoad() {
        super.viewDidLoad()

        someObject.delegate = self
        someObject.run()
    }

    // MARK: ADelegate
    func hi1() {

        print("Hi")
    }

    func hi2(message1: String, message2: String) {

        print("Hi \(message1) \(message2)")
    }
}

等价的是?接线员:

var value: NSNumber? = myQuestionableObject?.importantMethod()

importantMethod只在myQuestionableObject存在并实现它时才会被调用。

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替代品。

您可以通过以下方式进行检查:

someObject.someMethod?()

只有当someemethod方法定义在对象someObject上时,才会调用它,但你只能将它用于已声明该方法为可选的@objc协议。

Swift本质上是一种安全的语言,所以每次你调用一个方法时,Swift都必须知道这个方法在那里。不可能进行运行时检查。你不能对随机对象调用随机方法。

即使在Obj-C中,你也应该尽可能避免这样的事情,因为它不能很好地使用ARC (ARC会触发performSelector的警告:)。

然而,当检查可用的api时,你仍然可以使用respondsToSelector:,即使是Swift,如果你正在处理NSObject实例:

@interface TestA : NSObject

- (void)someMethod;

@end

@implementation TestA

//this triggers a warning

@end   

var a = TestA()

if a.respondsToSelector("someMethod") {
   a.someMethod()
}