0

It's not a dup of this question
I imported Objective-C files into a Swift project. But there is a login controller that has a callback invoking AppDelegate.
But the AppDelegate is a Swift file. According to the Apple's guideline, I added an ExampleProject-Swift.h header file then imported this header file in the login controller. But it doesn't seem to be the right way.

And in the AppDelegate.swift file

The code below is what I did in the ExampleProject-Swfit.h header.

#ifndef ExampleProject_swift_h
#define ExampleProject_swift_h

#import "AppDelegate.swift"
#endif /* ExampleProject_swift_h */

BTW, to use an Objective-C file, I already created an ExampleProject-Bridging-Header.h.
I don't know what's really going on here, anyone has any idea about how to solve this problem?
Thank you for reading my question.

JsW
  • 1,682
  • 3
  • 22
  • 34
  • http://pinkstone.co.uk/how-to-use-swift-classes-in-objective-c/ try checking this or https://ios.james.ooo/using-swift-in-objective-c-projects-f7e7a09f8be – iOS Geek Dec 28 '17 at 04:24

1 Answers1

1

The ExampleProject_Swift.h file is autogenerated by the compiler, so you shouldn't be adding your own.

Also, in the header file you created, you imported a Swift source file as if it was an Objective C header file, and the compiler isn't going to understand that. That is why you're getting all of those strange errors in the LoginViewController implementation file.

Dave Weston
  • 6,527
  • 1
  • 29
  • 44
  • This works. Thank you man. But I got another problem. I have some global macros, to use them in swiftI, I made some global `let` variables in a swift file, but the Objective-C file can't find those variables. What else should I do? In addition, in the old Objective-C project, I made a `PreFixHeader.pch` to import a file containing those macros. – JsW Dec 28 '17 at 05:17
  • The Swift compiler only handles very simple macros. If you want your Swift variables to be visible to Objective C, you'll need to make them public. – Dave Weston Dec 28 '17 at 08:40
  • 1
    Yeah, I've made it. The default internal access level is working. The automatically created Swift header, according to the Apple's document, will contain those marked by `internal ` modifier if there is an Objective-C bridging header. Thanks for you help, I was really confused about all this migration stuff. – JsW Dec 28 '17 at 09:18