Avoid compiling Swift files for a single target - swift

I have a large Xcode project, with a mixed Swift and Objective C codebase. One of my targets is a QuickLook plugin, which does not use any Swift files.
When building the QuickLook target, each Swift file with references to ObjC classes (correctly bridged in the main target) report "Cannot find type in scope" errors — even though none of those Swift classes are included in the given target.
I've tried changing SWIFT_COMPILATION_MODE to singlefile for both release and debug, but it didn't help. To be absolutely sure that no Objective C classes import any Swift files in this target, I've also set up a preprocessor macro to guard the inclusion of Project-Swift.h files. I can't find a flag to completely skip Swift compilation phase when building.
Are Swift they automatically compiled no matter what I do and regardless of their target membership?

For anyone else struggling with this in the future:
When QuickLook extensions are compiled, even for the simulator, it tries to compile any other targets that might include it. I had an old build target, which was no longer maintained. At some point, the bridging header of that target had fallen behind the actual project, and for some reason, Xcode threw an error for every Swift class.
Go through all any targets that include your extension, even tests, and make sure those compile correctly, or remove those altogether.
The errors were not associated with that particular target in the logs or in the UI, which is probably an Xcode bug.

Related

Building pure Swift Cocoa Touch Framework

I'm exploring Swift with Xcode-6 and so far so good although I think the new collections need a little bit of work as I've managed to break the compiler a few times.
Problem is I'm now stuck trying to create the framework package to then use in another project. The project builds without issue and all tests pass successfully. When I go to create Archive (which I assume is what is required) I receive the error:
:0: error: underlying Objective-C module 'Sample' not found
Now I assume this has something to do with the contents of my Sample.h which tells me
// In this header, you should import all the public headers of your framework using statements like #import <Sample/PublicHeader.h>
which is fine except I have only used swift enums, structs and classes for this framework so therefore no .h files exist.
Can anyone shed some light on this one as I can't find any documentation available yet?
EDIT (7/27/2018)
The information in the answer below may no longer be accurate. Your mileage may vary.
I sat down with an engineer and asked this exact question. This was their response:
Xcode 6 does not support building distributable Swift frameworks at this time and that Apple likely won't build this functionality into Xcode until its advantageous for them to do so. Right now, Frameworks are meant to be embedded in your app's project, rather than distributed. If you watch any of the session videos or see the sample projects, this is always the arrangement. Another reason is that Swift is very new and likely to change. Thus your Swift-based framework is not guaranteed to work on the platform you originally built it on.
Slight Update
The engineer mentioned that it's not impossible to make a framework for distribution, just that it's not supported.
I was able to get past the error by going to the target for the framework and on the Build Phases tab under Headers, remove the MyFramework.h file
However I was getting the "Underlying Objective-C module not found" error when I was using a framework to share code between a containing app and an app extension, both of which were pure Swift.
Assuming you are creating a truly pure Swift module, you must disable the Objective-C Compatibility Header and any generated interface headers so the compiler doesn't go off assuming it can find an Objective-C module for the framework.
Do Not remove your public framework header. You'll get a module-map warning at link time if you do.
You might find this useful: Creation of pure swift module
In short: it's possible to make static framework, but there is one issue: it doesn't work in end user' project if "ProjectName-Swift.h" header included.
But, it works fine if you use only swift.
I think it's a bug in XCode 6, or that Apple does not allow archiving the Framework from XCode while in beta.
If you compile to profile XCode generates the framework correctly. (See the Release folder created in DerivedData)

GenericKeyChain KeychainItemWrapper.m does not compile when copied to another project

I compile and run Apple's GenericKeyChain sample code. No problems there. Now I want to use both files KeychainItemWrapper.m and KeychainItemWrapper.h in my new project and I get lots of compilation errors. I see that Apple's sample code is using ARC. Other posting here direct people to disable ARC for this file. That did not work. I have link the Security Framework on my new project.
My question is what should I do to get these two files compiled successfully on my new project. Why Apple sample code compiles with no problems.
The compilation errors are mostly ARC Casting Rules and ARC restrictions as well as Semantic Issue of using "release".
try the following 2 methods:
Go to Build Setting -> Swift Compiler -> Objective-C Bridging Header and set the path to /"Your project name"-Bridging-Header.h
ex path: myProject/myProject-Bridging-Header.h
Go to Build Phase -> Compile Source and pick the objective-c file (.m file) and set compiler flag to be "-fno-objc-arc"

Library Project Reference in Xcode

I know something like this is already asked many times on SO, but I've tried everything (been at this for three hours now) and I still didn't find a solution. I'm quite new to xCode and I'm starting to work on a project that was originally created by somebody else.
So, I have a library project in my xcode (XS2Library) and now I would like to reference to that library in my other project (WaarBenJij). Building the Library succeeds and I reference to it by adding the LibXS2Library.a to the Project target's "Link Binary With Libraries".
Xcode doesn't give me any errors, so it looks like the library project is referenced appropriately. However, when I try to build my project I get the error that a class that resides in my library project cannot be found ("'XS2URLLoader.h' file not found").
Can anybody steer me in the right direction, maybe?
There's a sensible difference between the .a file, which is needed for linkage and contains the library source, and the .h file, which is needed to compile (and preprocess) and which contains the functions and classes declarations.
Here you included the .a file, which is required for a later step, but to use the library you also need to import the required .h files into your project.
The easier way is to simply put them in your project.
Or you can add the whole library as a subproject and as a dependency.

iPhone - XCode 4 linker error with no method call nor object use

I have a project that includes a shared library as source files.
Some of the library classes need the include of some Frameworks (like the known Apple Reachability).
Those Frameworks are not included into the project because I don't use the classes / methods that needs them.
I have a MyStandardLib.h that references all the library headers, and that is included in the.pch on my app.
But...
When building my app, the linker generates link error for those objects I don't use/call in the app So they should logicaly not be linked.
As an example, if I remove from the project the incriminated classes and Frameworks, everything works fine.
If I add the needed Frameworks :
It's crazy to do that for each project because of classes I won't use
It increases my app file size for nothing useful (26 Ko -> 1.2 Mo)
How can I avoid those linker error, and avoid links that should not happen :
keeping my library as is in the project (I don't want a precompiled library)
Not needing to include all those useless Frameworks until I really need them
The reason for those errors are clearly because the frameworks you are including in your project do require those other frameworks (from what you could provide they use: Reachability, the CFNetwork Framework and the CoreLocation Framework)
What is happening is that when the compile links your objects (in the compiler sense of it) it is also looking for objects 'mentioned' in your added frameworks and it is not finding them because they have not been included to your projects.
If you use a Framework A that is linked with Framework B then if you application C uses framework A it also needs framework B because frameworks are not exactly self contained.
If your code compile without those frameworks and your application needs to take low space simply remove them from your Build Phase and you'll be good to go.
However, if it doesn't compile without those frameworks you will have no choice but to include them in your project and, therefore, increasing even more the final space required by your application.

why do I get this error building a static library for my iPhone project using XCode 4?

I have an existing iPhone application, and I just wanted to make a static library out of the code, so that I can use it by a separate test application (within the workspace). The application compiles fine, but when I try to compile the library version (which has the same code files) I get the following error:
Lexical or Preprocessor Issue - "MyClass.h" file not found
However, the application still runs, even through there is this error. In fact when I compile the application (which uses the static library) it initially says no errors, and then after a second or two the above mention error then appears.
I'm basically doing:
Create a new target in the project using the Coco Touch Static Library
Add the .m files in the targets "Compile Sources"
Adding the .h files in the targets "Copy Headers"
Adding the same set of libraries in the "Link binaries with libraries"
Go into the Profile and modified the Target to be RELEASE
Any suggestions? Am I missing a step here?
I'll have a stab at this :-) I think it might be that MyClass.h has been set to private. Thus it is not added to header directory when you create the static library. You should check to see if this is so by checking the output header directory. If the header file is missing, then it's pretty sure thats whats going on.
Secondly, why use a seperate app for testing and therefore require building a static library at all? I have several static libraries and apps which I unit test with GHUnit. I do it by simply adding a new app target and configuring it to run the GHUnit iOS app. I can then add the classes I want to test and any testing frameworks such as OCMock without having to create separate projects. Basically it sounds like your testing methodolody is perhaps more complex than necessary. :-)
So, the issue turned out to be I had "-ObjC" set in the "Other Link Flags" option, which I'd put there as at one stage trying to get things working I read this was required - Dereks advice to review the compilation log worked well here