iOS error casting Object in Swift which inherited from an Obj-C Class

Viewed 167

We've discovered a problem in our project when inheriting from an Objective C class in a Swift class

ViewController.m

@implementation ViewController

+ (ViewController*) getObj {
    ViewController *obj = [[SwiftController alloc] init];
    return obj;
}

SwiftController.swift

    class SwiftController: ViewController { }

When testing the code the following test fails

    class Test: XCTestCase {
        func testFails() {
            let obj = ViewController.getObj()
            XCTAssertTrue(obj is SwiftController)
        }
    }

Implementing the same behaviour in swift only works fine.

    class FirstController { }
    class SecondController: FirstController {
        static func getObj() -> FirstController {
            let obj: FirstController = SecondController()
            return obj
        }
    }

    func testOk() {
        let obj = SecondController.getObj()
        XCTAssertTrue(obj is SecondController)
    }

Is this a swift/obj-c bug or does anyone have a solution for this behavior?

A copy of this sample project can be found on github: https://github.com/edgeftq/inheritanceTest.git

Cheers, Eddy

1 Answers
Related