Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I import Swift code to Objective-C?

I have written a library in Swift and I wasn't able to import it to my current project, written in Objective-C.

Are there any ways to import it?

#import "SCLAlertView.swift" - 'SCLAlertView.swift' file not found 
like image 492
Dark Matter Avatar asked Jun 08 '14 01:06

Dark Matter


People also ask

Can I use Swift library in Objective-C?

The Swift library cannot be directly called from Objective-C, since it is missing the required annotations in the code, and in many cases, modules do not inherit from NSObject, rather they use the native Swift data types.


1 Answers

You need to import ProductName-Swift.h. Note that it's the product name - the other answers make the mistake of using the class name.

This single file is an autogenerated header that defines Objective-C interfaces for all Swift classes in your project that are either annotated with @objc or inherit from NSObject.

Considerations:

  • If your product name contains spaces, replace them with underscores (e.g. My Project becomes My_Project-Swift.h)

  • If your target is a framework, you need to import <ProductName/ProductName-Swift.h>

  • Make sure your Swift file is member of the target

like image 120
Bill Avatar answered Sep 21 '22 08:09

Bill