Why we can override function defined in XCTestCase extension without @objc attribute

Why we can override the function in the XCTestCase extension without @obj

However, for the base class does not inherit XCTestCase, you need to add @objc.


XCTestCase inherits from XCTest (which inherits from NSObject), it doesn’t inherit directly from NSObject.

So in your example, it would be something like:

class A: NSObject {}

extension A {
  func hello() {}
}

class B: A {}

class C: B {
  override func hello() {} // ok
}

XCTestCase has a special attribute that makes all members @objc by default, because the test discovery mechanism on Apple platforms uses the ObjC runtime. So yes, XCTestCase is special. (You can still opt out with @nonobjc, and methods that aren't ObjC-compatible won't be exposed to Objective-C either.)

2 Likes