Can't Use Swift Classes Inside Objective-C

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



Leave a reply



Submit