Swift framework for Objective-C

Hello,


hopefully, this is a real beginer question, so hopefully it is easy to answer …


I want to create a new framework for an existing Objective-C app. And because Swift is all shiny, and we already are using Swift in other parts of the app, I am writing the framework in Swift.


However, when I #include "${MODULENAME}-Swift.h" in my umbrella header, Xcode tells me on a clean compile: ‘'${MODULENAME}-Swift.h' file not found'


If I comment that line, build, then uncomment it and build again, Xcode will find it. This, however, is unacceptable for, say, build servers.


Questions:


1) It appears, that when I am importing the Framework as a module (i.e. @import ${MODULENAME} ), I don't need to #include "${MODULENAME}-Swift.h" in my umbrella header.

Is this true? Is it not necessary to explicitly expose the -Swift.h headers when importing the Framework as a module?


2) This leads me to the next question:

Do Swift Frameworks have to be imported as modules? The app is an old codebase and currently sets CLANG_ENABLE_MODULES to NO. Is it necessary to set this to YES when using a Swift Framework?


3) Am I doing something wrong with the #import? If I can use the Framework not as a module, but #import <Framework/Umbrella.h> is good, what do I have to do to have{MODULENAME}-Swift.h available even on a clean compile?


Again, hopefully these are simple questions, and someone can easily answer them 😉

>However, when I #include "${MODULENAME}-Swift.h" in my umbrella header, Xcode tells me on a clean compile: ‘'${MODULENAME}-Swift.h' file not found'


Without also performing an 'option-clean build folder' following the addition of that include, that compile might not be as clean as you think.


Use Xcode's Product menu, then, with the option key pressed, select 'Clean Build Folder'. Confirm no errors in the navigator and go again. Problem is there is a CBF bug w/9.2 where this may fail silent, so be sure to use Xcode 9.3

Accepted Answer

The full instructions for this are here:


https://developer.apple.com/library/content/documentation/Swift/Conceptual/BuildingCocoaApps/MixandMatch.html


Note that there are different sections about different target types, since the procedure is slightly different in each case. You want the section titled "Importing External Frameworks", since your framework is external to the app. Most of the rest doesn't apply, because your framework is pure Swift.


>> Is it not necessary to explicitly expose the -Swift.h headers when importing the Framework as a module?


No, the bridging header is only relevant when you're mixing Swift and Obj-C in the same target.


>> Do Swift Frameworks have to be imported as modules?


Yes, according to the above document:


"When you import an external framework, make sure the Defines Module build setting for the framework you’re importing is set to “Yes”."


>> Am I doing something wrong with the #import?


Use @import, not #import.

URL is dead in the top answer :(
Swift framework for Objective-C
 
 
Q