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
,但它不起作用。以下是一个可以进行测试的 playground,它显示了在设置
@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