How can I import Swift code to Objective-C?
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
becomesMy_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
Importing Project-Swift.h into a Objective-C class...file not found
I was running into the same issue and couldn't get my project to import swift into obj-c classes. Using Xcode 6, (should work for Xcode 6+) and was able to do it in this way....
- Any class that you need to access in the .h file needs to be a forward declaration like this
@class MySwiftClass;
- In the .m file ONLY, if the code is in the same project (module) then you need to import it with
#import "ProductModuleName-Swift.h"
Link to the apple documentation about it
https://developer.apple.com/documentation/swift/imported_c_and_objective-c_apis/importing_swift_into_objective-c
swift build errors from Objective-C app due to import of Objective-C static library that contains a .swift file
The simplest solution is to allow Xcode to make all dependencies and include all required system libs.
Here is what I did:
Create workspace at top folder level
Added mobile_sensor_API.xcodeproj to workspace
Added sim_backend_UI.xcodeproj to workspace
Added dependency of sim_backend_UI to lib via workspace
- Add
Some.swift
(any swift file you want) to sim_backend_UI, just for the purpose Xcode add required system swift dynamic libraries (which will be needed for swift part in static library as well)... and confirm creating bridge in appeared dialog
- Build >>> Succeeded!
UPDATE 3 .................................. AFTER I DID THIS ANSWER thru step 6, I got "succeeded" but sim_backend_UI.app is RED and won't run on iphone.
Why can't I use my swift files in my objective-c code?
To clarify... Your class must be marked @objc
and must derive from NSObject
, and your func
must also be marked @objc
:
@objc class UserDefaultFactory: NSObject {
@objc func a() {
print("HELLO")
}
}
To then call that func from Objective-C code:
UserDefaultFactory *udf = [UserDefaultFactory new];
[udf a];
You could also call it via:
[[UserDefaultFactory new] a];
but more than likely you will be creating an instance of your UserDefaultFactory
object for additional usage.
Related Topics
Transfer Gestures on a Uiview to a Uitableview in Swift - iOS
What Is a Safe Way to Turn Streamed (Utf8) Data into a String
Custom Mkoverlayrenderer Drawmaprect Function Not Drawing Polygons
Pure Swiftui Login, Signup, Register Flow, Is It Possible
Attempting to Segue from Objective-C to Swift Vc
Cannot Access Appdelegate While Testing Xcode Project
Snapshot of Swiftui View Is Partially Cut Off
Why Obj-C Instance Have 1 Retain Count Just Created
Displaying Instances of View Controllers Within Xcode Without Disturbing the Current Hierarchy
Avaudioplayer Working on Simulator But Not on Real Device
Scene Kit Memory Management Using Swift
How to Cluster Custom Icons Markers in Googlemaps for iOS
Can't Dismiss Navigation Controller in Swift
Completely Unrelated Error Using Coredata
Please Add the Host Targets for the Embedded Targets to the Podfile