6

I made this simple extension in Swift:

extension DispatchQueue {
    func asyncAfter(delay: TimeInterval, block: @escaping ()->()) {
        DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + Double(Int64(delay * Double(NSEC_PER_SEC))) / Double(NSEC_PER_SEC), execute: block)
    }
}

In Project-Swift.h header it reports error at this line:

@interface OS_dispatch_queue (SWIFT_EXTENSION(...))
- (void)asyncAfterDelay:(NSTimeInterval)delay block:(void (^ _Nonnull)(void))block;
@end

Error is: Cannot find interface declaration for 'OS_dispatch_queue'

Is there a way to prevent extension being exported for Objective-C? Or is there a way to fix the error?

Dharmesh Kheni
  • 71,228
  • 33
  • 160
  • 165
Matej Ukmar
  • 2,157
  • 22
  • 27

2 Answers2

0

I know this is not an answer per say, but i encounter the same problem using the public extension DispatchQueue on: dispatch_once after the Swift 3 GCD API changes

So on my case using the solution from Vlad helped me avoiding this constant problem: https://stackoverflow.com/a/41570198/1672521

Wilson Muñoz
  • 191
  • 5
  • 7
-3

You can use @objc before the func or method to prevent being exported for objective-C like below.

extension DispatchQueue {
    @objc func asyncAfter(delay: TimeInterval, block: @escaping ()->()) {
        DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + Double(Int64(delay * Double(NSEC_PER_SEC))) / Double(NSEC_PER_SEC), execute: block)
    }
}
Bhautik Ziniya
  • 1,554
  • 12
  • 19
  • 2
    its '@noobjc' for preventing ... but it doesn't solve the problem because class 'DispatchQueue' is still exported to 'OS_dispatch_queue' which is unknown to Objective-C and it still fails. – Matej Ukmar Jan 31 '17 at 07:27