How to Import Existing Objective C Classes in Swift

How to use Swift classes in Objective-C files in framework?

FYI the solution to this issue is just changing

#import "ProjectName-Swift.h"

into

#import "ProjectName/ProjectName-Swift.h"

import swift linked objc class in swift

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.

Adding objective c class that uses swift classes to bridging header Projectname_swift.h not found

a circular reference has been created, making it so the Swift code is unable to compile (which leads to the canary error stating that the _Swift.h file is not found).

i have provided a more in depth answer to a similar questions here and here.

long story short, the documentation explicitly says not to this:

To avoid cyclical references, don’t import Swift code into an Objective-C header (.h) file. Instead, you can forward declare a Swift class or protocol to reference it in an Objective-C interface.

Forward declarations of Swift classes and protocols can only be used as types for method and property declarations.

in order to make your code compile again you will need to remove the #import "Projectname_Swift.h" line from the offending Objective-C header. ideally you can simply move the import statement into your .m file, however if you need to publicly expose the Swift class in your ObjC header, then you must forward declare it using @class SomeSwiftClass;.

iOS Swift project, already imported Objective-C file and I want to import a swift class into this Objective-C

Very Simple. Just declare @objc public before the swift class. Like this

@objc public class YourSwiftClass : NSObject {

}

Now in the objective c class use #import "YourProjectName-Swift.h". You will get error that the mentioned file was not found. Don't worry, just run the project once. When you click on the file at runtime you will see the contents of "YourProjectName-Swift.h" . Now you can access any methods of the swift class.



Related Topics



Leave a reply



Submit