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.
Can't access swift class from Objective-C while the project is in Swift
You should import your swift module into your .m file with this syntax:#import "ModuleName-Swift.h"
where ModuleName
usually is your Project Name.
NOTE: adding -Swift
suffix is important!
Also @class DataManager;
will be redundant after import.
Swift Class can not be reached in Objective-C class?
I have solved the problem. Here, I would like to introduce this specific case for those who may encounter with such a problem in the future.
The objective-c file is in target membership with two projects. The Swift file was in target membership with one of them. I have assigned this Swift file to the second project. It compiles now.
Related Topics
React-Native, How to Get File-Asset Image Absolute Path
Remove Alpha Channel in an Image
How to Force Nslocalizedstring to Use a Specific Language
How to Take a Screenshot Programmatically on Ios
How to Present Uialertcontroller When Not in a View Controller
Why Is There Extra Padding At the Top of My Uitableview With Style Uitableviewstylegrouped in Ios7
How to Create a Uicolor from a Hex String
Detect If the Device Is Iphone X
How to Get the Root Directory of My iOS App
How to Create Custom Calendar in React Native
How to Pass Prepareforsegue: an Object
Performselector May Cause a Leak Because Its Selector Is Unknown
How to Load Custom Uitableviewcells from Xib Files