美文网首页
swifter 0 — selector 使用示例

swifter 0 — selector 使用示例

作者: 闲鱼尼克 | 来源:发表于2018-02-26 12:58 被阅读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:]
    

    相关文章

      网友评论

          本文标题:swifter 0 — selector 使用示例

          本文链接:https://www.haomeiwen.com/subject/kufyxftx.html