swifter 0 — selector 使用示例

2018-02-26  本文已影响18人  闲鱼尼克

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:]
上一篇下一篇

猜你喜欢

热点阅读