protocol LazyUpdateable {
func waitToDoStuff()
func myMethodName()
}
extension LazyUpdateable where Self: NSObject {
func waitToDoStuff() {
self.performSelector(#selector(myMethodName), withObject: nil, afterDelay: 1.5)
}
func myMethodName() {
}
}
Với bản cập nhật này, tôi gặp lỗi Argument of #selector refers to a method that is not exposed to objective c
, nhưng nếu tôi đi với cũ Selector("myMethodName")
tôi nhận được cảnh báo để thay đổi cách làm tốt hơn. Có thể sử dụng số #selector()
trong trường hợp này không? Nó sẽ không hoạt động với thiết lập @objc
trên giao thức của tôi, tôi đã thử nó.Làm thế nào để sử dụng #selector (myMethodName) trong một phần mở rộng giao thức?
Đây là một sân chơi bạn có thể thử cho thấy nó không hoạt động với thiết @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