【发布时间】:2016-03-23 09:55:26
【问题描述】:
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
【问题讨论】: