Search code examples
iosobjective-cswift3objc-bridging-header

import swift linked objc class in swift


The project I work currently has Both Objective-C and Swift linked with the bridging header and everything works fine. but in one case where this particular viewController.h which is in Obj-c has swift imported already. when I try to access this viewController.h in another Swift controller it says '<#project-name#>-swift.h' file not found

Guide me how can I access this objC file in swift :(

Edited: detailed!!

I have a viewController.h file where I have implemented swift models by importing '<#project-name#>-swift.h'

but when I try to access this viewController.h from SecondViewController.swift, I have to add this #import "viewController.h" to the bridging header of my project. If I do so I am getting this error '<#project-name#>-swift.h' file not found


Solution

  • If my understanding of what you are doing is correct, the problem is that the projectname-Swift.h header is included in a header (viewController.h) that is in turn imported in the bridging header.

    The documentation at https://developer.apple.com/library/content/documentation/Swift/Conceptual/BuildingCocoaApps/MixandMatch.html#//apple_ref/doc/uid/TP40014216-CH10-ID122 talks about including the generated *-Swift.h header in .m files, not in headers, suggesting it should not be included in headers to avoid circular dependencies. You can still get away with including it in a header, but that will break if the header is included in the bridging header.

    I would import *-Swift.h in viewController.m, not viewController.h. If you need to reference a Swift type as a property, argument, or return type in viewController.h, then you can use forward declarations, like

    @class MySwiftModelClass;
    

    If classes declared in viewController.h extend MySwiftModelClass, then things get a little trickier. Please post a more specific brief example if that is the case.