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
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;
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.
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
.
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
Xcode version 10.2.1
Follow Create Swift framework
Expose Swift API. To use Swift's functions from Objective-C [About]
Follow Using Swift framework
Import module to the Objective-C client code [module_name]
@import module_name;
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
Xcode version 10.2.1
Create a framework project or create a framework target
File -> New -> Project... -> Cocoa Touch Framework
//or
Project editor -> Add a Target -> Cocoa Touch Framework
Two files will be generated:
Info.plist
- Build Settings -> Info.plist File
<product_name>.h
- Build Phases -> Headers
. It is umbrella header file [About] Add files .swift
Select `.swift` file -> Select File Inspectors Tab -> Target Membership -> Select the target
//or
Project editor -> select a target -> Build Phases -> Compile Sources -> add files
Expose Swift API. To use Swift's functions from Objective-C [About]
Build library - ⌘ Command + B or Product -> Build
Note: Be sure that you build the framework for the same process architecture as the client code.
Find generated output [Build location]
Products group -> <product_name>.framework -> Show in Finder
The framework includes
Info.plist
Modules
folder with:
module.modulemap
[About] [Custom modulemap] This file was autogenerated because Build Settings -> Defines Module -> YES
<product_name>.swiftmodule
folder with
.swiftdoc
- docs .swiftmodule
- public interface/definitions Headers
folder with:
Headers
section. There are public interfaces/definitions <product_name>-Swift.h
- Xcode-generated header file [About] Drag and drop
the binary into the Xcode project [About]
Embed binaries
[Library not loaded] [Link vs Embed]
Project editor -> select a target -> General -> Embedded Binaries -> path to `<product_name>.framework` file
I will automatically add the framework to:
Project editor -> select a target -> General -> Linked Frameworks and Libraries
Project editor -> select a target -> Build Phases -> Embed Frameworks
Project editor -> select a target -> Build Phases -> Link Binary With Libraries
Add Framework Search paths
[Module not found] [Recursive path]
Project editor -> select a target -> Build Settings -> Search Paths -> Framework Search paths -> add path to the parent of `<product_name>.framework` file
Import module to the Objective-C client code [module_name]
@import module_name;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.