Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

objective-c - Swift classes are not visible in Objective-C .h file

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??

like image 810
Oleg Avatar asked Jan 30 '15 21:01

Oleg


People also ask

Can we use Swift class in Objective-C?

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.

What is Swift .h file?

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.


2 Answers

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

like image 172
Eyeball Avatar answered Oct 20 '22 11:10

Eyeball


Remember to import the project generated swift header file in your source file, for example #import <MyProjectName-Swift.h>

like image 23
andrewz Avatar answered Oct 20 '22 11:10

andrewz