swifter 0 — selector 使用示例

01 调用 有参数名

// 无参数
@objc func callMe() {
}
// 一个参数
@objc func callMeWithParam(obj: AnyObject!) {
}
 
// 多个参数
@objc func turn(by angle: Int, speed: Float) {
}

func selectors() -> [Selector] {
    let someMethod = #selector(callMe)
    let anotherMethod = #selector(callMeWithParam(obj:))
    let method = #selector(turn(by:speed:))
    
    return [someMethod, anotherMethod, method]
  }

打印selectors的结果

[callMe, callMeWithParamWithObj:, turnBy:speed:]


02 调用 无参数名

func otherSelectors() -> [Selector] {
    let someMethod = #selector(callMe)
    let anotherMethod = #selector(callMeWithParam)
    let method = #selector(turn)
    
    return [someMethod, anotherMethod, method]
  }

打印selectors的结果

[callMe, callMeWithParamWithObj:, turnBy:speed:]


03 调用 加 函数类型

  @objc func commonFunc() {
    
  }
  
  @objc func commonFunc(input: Int) -> Int {
    return input
  }
  
  func sameNameSelectors() -> [Selector] {
    let method1 = #selector(commonFunc as ()->())
    let method2 = #selector(commonFunc as (Int)->Int)
    return [method1, method2]
  }

打印selectors的结果

[commonFunc, commonFuncWithInput:]
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容