Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

importing swift framework into a objective-c project

I am importing swift framework into objective-c project like this:

@import MyFramework;

The problem is that only some of the classes are recognized by the class i am importing the framework.

The class which is recognized:

public class RecognizedClass:UIViewController, WKNavigationDelegate, WKScriptMessageHandle 
 { ... } 

The class which is not:

public class VeediUtils
{ ... } 

They are both public so why the first is recognized in the workspace and the other not?

Also i see in the header file MyFramework-Swift.h that a class

@interface RecognizedClass : UIViewController <WKNavigationDelegate, WKScriptMessageHandler>

Appear while the other dont

Why is that?

Also to point that this same procedure work when i am importing swift framework to swift project

like image 800
Michael A Avatar asked Jun 28 '15 12:06

Michael A


People also ask

How do I add Swift to Objective-C project?

To access and use swift classes or libraries in objective-c files start with an objective-c project that already contains some files. Add a new Swift file to the project. In the menu select File>New>File… then select Swift File, instead of Cocoa Touch Class. Name the file and hit create.

Can Swift protocol be used in Objective-C?

In order to use Swift code inside Objective-C one must scrifice some Swift features and write a wrapper around original Swift code that won't use non-compatible features (like structs, generics, enum associated values, protocol extensions etc.). All wrapper classes must inherit NSObject .

How do you subclass a Swift class in Objective-C?

Unfortunately, it's not possible to subclass a Swift class in Objective-C. Straight from the docs: You cannot subclass a Swift class in Objective-C. See Apple's guide on interoperability for more details on what you can and cannot access with Objective-C.

Does Swift use the Objective-C runtime?

Even when written without a single line of Objective-C code, every Swift app executes inside the Objective-C runtime, opening up a world of dynamic dispatch and associated runtime manipulation.


4 Answers

If you previously configured Project for integrating with Swift and want to use Swift Dynamic Framework, you have to import it like this way (replace {value} with appropriate names depending on your Project):

#import <{MyFramework}/{MyFrameworkMainClass}-Swift.h>  
#import "{YourProjectTargetName}-Swift.h"

EDIT:

If your framework has Defines Module set to true, then you can import it like this:

@import MyFramework;
like image 168
saltwat5r Avatar answered Oct 21 '22 14:10

saltwat5r


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

like image 40
fabianfett Avatar answered Oct 21 '22 13:10

fabianfett


You have to add @objc to the declaration of the VeediUtils class, or make it inherit from NSObject. Otherwise it won't be visible to Objective-C.

In your case, RecognizedClass is recognized because it is a subclass of UIViewController, which is a subclass of NSObject.

like image 40
Cihan Tek Avatar answered Oct 21 '22 12:10

Cihan Tek


Using Swift Classes in Objective-C

If you are going to import code within an App Target (Mixing Objective-C and Swift in one project) you should use the next import line #import "<#YourProjectName#>-Swift.h" to expose Swift code to Objective-C code [Mixing Swift and Objective-C code in a project]

In this post I will describe how to import Swift framework to Objective-C code

Objective-C consumer -> Swift dynamic framework

Xcode version 10.2.1

Create a Swift framework

Follow Create Swift framework

Expose Swift API. To use Swift's functions from Objective-C[About]

Objective-C consumer

Follow Using Swift framework

Import module to the Objective-C client code[module_name]

@import module_name;

[More examples here]

like image 41
yoAlex5 Avatar answered Oct 21 '22 14:10

yoAlex5