Importing Swift Framework into a Objective-C Project

importing swift framework into a objective-c project

To access a swift class in objc, that is not inherited from NSObject you need to:

@objc public class VeediUtils

A Swift class or protocol must be marked with the @objc attribute to be accessible and usable in Objective-C. This attribute tells the compiler that this piece of Swift code can be accessed from Objective-C. If your Swift class is a descendant of an Objective-C class, the compiler automatically adds the @objc attribute for you.

https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/BuildingCocoaApps/MixandMatch.html

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"

Using a Objective C framework inside a Xcode Swift Project

The most easy way without digging to deep into settings is..

  1. In your Swift project create one Objective-C Class (.m+.h) file, the naming is not important. This file will be needed anyway so you can code more in objective-c for your project.

  2. When you are asked to generate Bridging Header, say yes.

  3. In the generated Projectname-Bridging-Header.h (not your own created file)
    place your #import <SDF/SDF.h> rule.

  4. Compile once.

  5. Start programming in swift with your ObjC stuff.

Alternative: go into your target settings or project settings and search for "bridging" and change the parameters as you need.
There is (A) one way bridging to swift and (B) one way auto generated bridging into objective-c. Both name conventions can be edited but only one Projectname-Bridging-Header.h will be visible in your project file tree. The other one Projectname-Swift.h is repeatedly generated from Xcode for you. If you try to expose functions from Swift to Objective-C, this "invisible" header file needs to be imported as well, but in the Objective-C file you need it via #import "Projectname-Swift.h".



Related Topics



Leave a reply



Submit