I have a project with several targets. For most of my classes, I can use a single .h file and change the details in the .m file (adding a different .m file for each target).
For one of my UIViewController subclasses, I need to declare different UILabel IBOutlets for the different targets (each target shows a different set of labels depending on what's available). The problem is that header files can't be targeted. There's no checkbox next to them to specify target membership.
The way I've been dealing with this is to just add outlets for all of the targets and ignore the unused ones. It doesn't seem ideal.
Do I need to use a header build phase for this? What's the best way to deal with this problem? Thanks.
Edit: I should have mentioned that I want all the .h files to have the same name: PlaceViewController.h.
You can use preprocessor directives to selectively include the header files. For instance
#if TARGET_OS
#import "FirstTarget.h"
#else
#import "SecondTarget.h"
#endif
You can use different folders for different targets if the headers are the named the same:
#if TARGET_OS
#import "First/Target.h"
#else
#import "Second/Target.h"
#endif
You can read more about conditionals here.
Headers don't get compiled themselves, but are included in other files which are then compiled. You can use conditional compilation inside those files to include the correct headers for the target. Define a unique symbol in the preprocessor symbols for each target, and include the appropriate headers accordingly:
#if defined(TARGET_ONE)
#include "HeaderOne.h"
#elif defined(TARGET_TWO)
#include "HeaderTwo.h"
#endif
I'm sorry to be answering a couple of years too late, but I recently had the same problem, and there's a way to do this:
Let's say you already created a new target by duplicating the original target. Right-click the first (original) .h file in the Project Navigator and open the file in Finder.
In Xcode, right-click the file, and remove it from the project (only remove reference, do not move to trash).
In Finder, create two folders (you can't have two same-named files in a single folder), and move/copy the original .h file in both of them.
Now, drag-and-drop the first folder somewhere in your project, and when the window with the targets pops out, select only the first target.
Same with the second folder, select only the second target.
VoilĂ , you should now have two same-named .h files.
Caveats: it seems Xcode has random problems if your .h file has an NS_ENUM. It is probably solvable, but I still haven't figured it out.
Related
I'm mainly refering to application-level imports, not when you build a library, in which case is obvious why you should avoid importing the headers inside the .pch file.
The point of the .pch file is that it can be precompiled once and left alone.
If you import all of your application headers in the .pch, it has to be recompiled every time you change any one of those headers, negating all of the benefits. Worse yet, this ends up meaning that every other file in your project has to be rebuilt, because the contents of the .pch changed.
The only headers that should be pulled in by your .pch are headers that'll almost never change, like system headers.
Adding all of your imports to the pch might make your build slower. It really depends on the project.
Let's say you have 100 .m files, and one of them uses AVFoundation. If you add the AVFoundation header to your pch, then the compiler has to deal with the larger pch for every .m file, but only one of them benefits from it. This might be slower or it might not. It's probably too small a difference to measure easily.
If you are thinking of adding your project's own .h files to the pch, that will definitely make your build slower, because Xcode will recompile every .m file when any one of your .h files changes.
Another reason to include a framework header only where it's needed is to make your source files easier to reuse if you decide to copy them to another project.
I was reading an old post on here: Constants in Objective-C which talks about how to set up a file for your constants when doing iPhone dev in objective C. One of the responses (which got a lot of helpful votes) says that "Constants.m should be added to your application/framework's target so that it is linked in to the final product."
My question is, how do you do this, and what is accomplished by this? Does this make it so you don't have to import the constants file in the files you need to use it for?
Also, does the same method talked about in the response apply to integer constants, etc?
Thanks.
It makes it so that the constant values are available in the linked binary. Your header file will be included by reference (since you #import it) but the compiler needs to know that you want that .m file to be part of the final binary.
In XCode 4, choose your project from the top of the Project Navigator (the file tree view tab on the left). Select your target from the list of targets that appears in a new column to the right of the Project Navigator. Select the Build Phases tab on the right. Open up the Compile Sources item. Here are all of the files that are being compiled to build your binary. Make sure your constants file is in here. If it is not, find it in the Project Navigator and drag it into the Compile Sources pane.
In Xcode 4, beside the method Seamus mentioned, you can also select that .h file, open right view (at the top-right conner, there're three), in the first tab there's target membership, select the target you want to add it to and you're set.
You have to make sure you #import and also the file has to be in build target. #import make sure your complier know where to look for, include the file in target make sure when complier go and look, there's that file (The file won't be include in your build if it's not part your building target even it's in your project).
i dont know what to do. I cannot add any .H file to Target in my projects. I tried add it from Add FIle, from drag and drop then info file and check target. Its greyed. when i try to add new file and choose H a cannot check to add it to target. whats is wrong?
Headers are not compiled, so they don't get added to the target.
Look at all the headers currently in your project like your XxxxxAppDelegate.h, you'll see it's not added to the target either.
the header files are not compiled into the project, only the source files and frameworks. The header files are only there to tell the compiler what it can expect in regards to sending messages to objects
I have following issue: I have an iPhone application which can be compiled using various color styles. So the goal is to have several build configurations with defined style, for instance COLOR_STYLE_BLACK. Style definition files should be placed under some subfolder in source tree, like, for example Classes/styles/black. Then, in App_Prefix.pch I'd like to #import files from respective subfolder, like following:
#ifdef `COLOR_STYLE_BLACK`
#import "styles/black/DefaultStyle.h"
#endif
But the issue is that I cannot make XCode to import files from subfolders. It seems XCode does not allows folder structures in project, or at least I cannot figure out how to do it.
When I add folders as folder references to the project, XCode copies them to the Rersources folder, but does not add them to Compile sources build phase and reports errors on missing files.
Thanks for any tip or advice on this.
Matthes
The bigger issue here is that you're hardcoding your theme in sourcecode.
Its only ever the most extreme customisation that should be done this way. Its sound advice that your theme should be in artwork and you should select it at runtime by varying the path you load the artwork from and such.
I'm referencing a static library.
I've dragged the library project into my app project and reference it relatively.
The .a file from the library is linked into my app target.
I've added a reference to the librarys .h files' folder via Header Search Paths in the app project.
Then I add an import in my apps .pch file to a particular .h file from the library.
I build and get this error:
some.h: No such file or directory
The project is given info on where to find .h files for the library. Why can't it see them?
Try with
#import <some.h>
instead of
#import "some.h"
I don't think so. I just got one set up today piecing it together from the links below. I didn't include any .h files in search paths. In fact the one tutorial told me to remove any existing search paths (but I had to add them back for another library). Hope this gets you on the right track.
http://www.clintharris.net/2009/iphone-app-shared-libraries/
http://blog.stormyprods.com/2008/11/using-static-libraries-with-iphone-sdk.html
http://blog.costan.us/2009/02/iphone-development-and-code-sharing.html