Xcode 6 Swift + Objective-C Build Issues - Unknown Types Due to Import Order?

It is very difficult for me to structure a mixed iOS application for Swift / Obj-C to avoid build errors after converting classes to Swift.

This is a problem with a very brief break in the Swift documentation:

If you use your own Objective-C types in your Swift code, be sure to import the Objective-C headers for these types before importing the Swift generated header into the Objective-Cm file that you want to get the Swift code for.

Firstly, although the documentation pretty explicitly states that the -Swift.h file is in .m files, it very often refers to types from classes that are now based on Swift code in .h files of Obj-C classes, so I have to import -Swift.h to .h files most of the time. It seems strange to contradict the document, but I don’t see how else these headers will be built, and this worked most of the time.

The real problem occurs when the generated -Swift.h file contains compiler errors, such as

Unknown type name 'SomeObjCClass'

The only way to overcome these errors is to make sure that the specified class reaches the -Swift.h file, as the documentation suggests, but is unable to figure out how the build order is determined. I find I am importing all kinds of Obj-C headers throughout the project until the generated header, until I get the working assembly. There is no way for this to be a good way, but I don’t know the right way.

How to determine where the -Swift.h header -Swift.h included for the first time, so that I can get all the necessary Obj-C classes imported just above it, and then it will be enough so that only -Swift.h can be imported in the remaining places as necessary, or should its prerequisites be collected above it every time (which, I believe, is not much different from what I'm doing now)?

+6
source share
1 answer

In my experience so far, to use the Objective-C classes in Swift, I just included these .h files in the -Bridging-Header.h and for Swift, which should be used in Objective-C code, I include -Swift.h into these .m files.

I had no problems with this unknown type. And all my references to Swift classes are contained in .m files. But if there is a Swift class, you need to reference it in the .h file, then you should just make an announcement ahead of @class MySwiftClass .

0
source

Source: https://habr.com/ru/post/971185/


All Articles