I'm trying to use Swift class in my Objective-C file.
I included "moduleName-Swift.h"
file in my SuperViewController.m
file, but when I try to declare a public method in SuperViewController.h
with Swift-class
as method parameter, I obviously get an error: "Expected a type"
How can I use swift-class
in a header file if I can only include projectModule-Swift.h
in .m
files??
You can work with types declared in Swift from within the Objective-C code in your project by importing an Xcode-generated header file. This file is an Objective-C header that declares the Swift interfaces in your target, and you can think of it as an umbrella header for your Swift code.
h is for objc based project to use swift code I remember. It is generated by XCode, to translate swift code to a header file for objc to import. If you don't need to use swift in objc project, you don't need it. As you can see you are simply a swift project, everything is swift.
Make sure to put @objc before the swift class name.
@objc myclassname { ... }
Also add
@class myclassname;
in your obj-c header file where you want to access the swift class
Remember to import the project generated swift header file in your source file, for example #import <MyProjectName-Swift.h>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With