Reputation: 1306
I want to access swift class(variables and methods) in my objC class.
I have created a YourProjectName-Swift.h file.
#import "YourProjectName-Swift.h"
My swift class is:
@objc public class MySwiftClass: UIView {
override init(frame: CGRect) {
super.init(frame: frame)
addBehavior()
}
required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
addBehavior()
}
func addBehavior() {
print("Add all the behavior here")
}
}
How can I access this class from an Objective-C file?
I tried to initiate my swift class object with the below, but it's not working.
MySwiftClass *swift = [[MySwiftClass alloc] init];
Upvotes: 0
Views: 826
Reputation: 11039
You shouldn't create YourProjectName-Swift.h file. It will be generated automatically for you!
Just import YourProjectName-Swift.h in your ObjC class where you want to access swift class, and use it.
Considerations:
If your target name contains spaces, replace them with underscores (e.g. Your Project Name becomes Your_Project_Name-Swift.h)
If your target is a framework, you need to import <YourProjectName/YourProjectName-Swift.h>
Upvotes: 0