如何在协议扩展中使用#selector(myMethodName)?

时间:2016-03-23 09:55:27

标签: swift2 protocols

protocol LazyUpdateable {
    func waitToDoStuff()
    func myMethodName()
}


extension LazyUpdateable where Self: NSObject {
    func waitToDoStuff() {
        self.performSelector(#selector(myMethodName), withObject: nil, afterDelay: 1.5)
    }

    func myMethodName() {

    }
}

通过此更新,我收到错误Argument of #selector refers to a method that is not exposed to objective c,但如果我使用旧版Selector("myMethodName"),我会收到警告,要求更改为更好的方法。在这种情况下是否可以使用#selector()?它不能在我的协议上设置@objc,我已经尝试过了。

以下是您可以尝试的操场,显示它不适用于设置@objc

import Foundation
import UIKit
import XCPlayground


@objc protocol LazyUpdatable {
    optional func waitToDoStuff()
    optional func myMethodName()
}

extension LazyUpdatable where Self: UIViewController {
    func waitToDoStuff() {
        self.performSelector(#selector(myMethodName), withObject: nil, afterDelay: 1.5)
    }

    func myMethodName() {
        print("LOL")
    }
}


@objc
class AViewController: UIViewController, LazyUpdatable {
    func start() {
        waitToDoStuff()
    }
}

let aViewController = AViewController()
aViewController.start()

XCPlaygroundPage.currentPage.needsIndefiniteExecution = true

2 个答案:

答案 0 :(得分:3)

使用#selector()或Selector()进行动态调度并没有看到你的Swift协议扩展。相反,如果可能的话,尽量避免使用Objective-C。您可以使用libdispatch获得相同的结果:

protocol LazyUpdatable {

    func waitToDoStuff()
    func myMethodName()
}

extension LazyUpdatable {

    func waitToDoStuff() {
        let dispatchTime = dispatch_time(DISPATCH_TIME_NOW, Int64(1.5 * Double(NSEC_PER_SEC)))
        dispatch_after(dispatchTime, dispatch_get_main_queue()) {
            self.myMethodName()
        }
    }

    func myMethodName() {
        print("Aloha!")
    }
}

class ViewController: UIViewController, LazyUpdatable {

    override func viewDidLoad() {
        super.viewDidLoad()
        waitToDoStuff()
    }
}

当然,这不像使用选择器那样灵活,但允许您使用真正的Swift协议扩展。

修改 如果您希望能够取消调用方法调用,请尝试以下操作:

var lazyUpdatableCancelKey = UInt8(0)

protocol LazyUpdatable: class {

    func waitToDoStuff()
    func cancelDoingStuff()
    func myMethodName()
}

extension LazyUpdatable {

    func waitToDoStuff() {
        let dispatchTime = dispatch_time(DISPATCH_TIME_NOW, Int64(1.5 * Double(NSEC_PER_SEC)))
        dispatch_after(dispatchTime, dispatch_get_main_queue()) {
            if let shouldCancel = objc_getAssociatedObject(self, &lazyUpdatableCancelKey) as? Bool where shouldCancel == true {
                return
            }
            self.myMethodName()
        }
    }

    func cancelDoingStuff() {
        objc_setAssociatedObject(self, &lazyUpdatableCancelKey, true, .OBJC_ASSOCIATION_RETAIN_NONATOMIC)
    }

    func myMethodName() {
        print("Aloha!")
    }
}

class ViewController: UIViewController, LazyUpdatable {

    override func viewDidLoad() {
        super.viewDidLoad()
        waitToDoStuff()

        let dispatchTime = dispatch_time(DISPATCH_TIME_NOW, Int64(1.4 * Double(NSEC_PER_SEC)))
        dispatch_after(dispatchTime, dispatch_get_main_queue()) {
            self.cancelDoingStuff()
        }
    }
}

答案 1 :(得分:1)

对于XCode 8.2.1和Swift 3。

您可以使用您尝试过的传统Selector("methodName")。此外,您可以通过将警告包含在括号Selector(("methodName"))

中来使警告静音

由于一切都在您的协议范围内,因此您不太可能制造最终会崩溃的拼写错误