In Objective-C, importing same headers in every class make compile time longer? - iphone

I'm a beginner of Objective-C/iOS programing.
I want make a one header file which includes all class headers I use in my project.
And import the header in every class header file.
Like this question:
Including multiple classes in the same header file
But does this approach increase compile time?
Or are there any other disadvantage?
Please tell me the good ways to import headers.

In general, newly-generated iOS projects come with this functionality, which is called a precompiled header or prefix header, and is a file that has the extension .pch.
You can throw all the headers you want in there and Xcode will pre-compile it before it builds anything else, and use it to compile the other compilation units in your project (e.g. .m files).
Using a precompiled header may or may not increase compile time; in general, it reduces compile time, as long as you have a lot of common headers and/or a lot of source files.
However, it's not necessarily good practice to treat the pre-compiled header like a big dumping ground, as your compilation units can form implicit dependencies on all sorts of stuff when you may want to enforce loose coupling between components.

The issue with all the classes in one header is that every time you change a class header then all the files including it even indirectly will need to be recompiled, whilst if you only import the needed class and also use #class when you can then only the files that directly use the class need to be recompiled. Thus in the first case there will be many more compilations than in the latter. This is the way I would recommend to start.
However when your code becomes more stable and classes do NOT change then putting them all in one header can improve the compile time as the precompiled header will contain the same information for each file. What I would do is when the code is not changing so much is put the mature classes into a Framework and the Framework header will include all these classes.

You can import that header file in your projects prefix_pch file.then You can use it in Your classes .

If you want headers imported globally you should do so in the YourProject-Prefix.pch file. It should look something like this..
#import <Availability.h>
#ifndef __IPHONE_4_0
#warning "This project uses features only available in iOS SDK 4.0 and later."
#endif
#ifdef __OBJC__
#import <UIKit/UIKit.h>
#import <Foundation/Foundation.h>
#import "YourGlobalHeader.h"
#endif
Now, All of your classes have YourGlobalHeader.h automagically imported.

Putting all the headers in one file may improve build performance in certain cases, but you probably won't notice the difference.
It is better to keep your class headers in different files for purposes of organization. Also, if you are only including the headers that you need in your source files then your build time will be reduced, although again not noticeably if you are using a decent build machine.

Related

Even if I don't use obj-c, do I have to make a header file to modularize the app?

I'm studying a clean architecture project.(https://github.com/sergdort/CleanArchitectureRxSwift) In this project, each directory creates a header file and references the class through a header file in a different directory file. I think they made it like this for modularity. I searched for it, and it seems that the header file is only used to insert the obj-c code into the Swift code. Is it common to make header files just for modularity without using obj-c?
Application.swift
Domain.h

Sqlite for custom swift framework

I am creating a custom swift framework for managing all common data of my applications like data base or common classes.
I import the library "libsqlite3.0.tbd" in Linked Frameworks and Libraries, but when I try to put #import "sqlite3.h" in my Umbrella header file, I recite the next error:
Include of non-modular header inside framework module
Any idea?
Thank you very much
Step by Step what I did with my sqlite project for it to work, try this (keep the library added and the #import statement in the header):
First, remove the Header from Build Phases >> Headers.
Then go to Build Settings and add the path to the Header in the Objective-C Bridging Header. If you just created the Framework, didn't change the name of the .h file and your project is called Test, for example, the path will be Teste/Test.h (Xcode 7.1.1).
Hope it works.

How to hide headers when packaging static library?

I want to put my private headers into my static library, so the customers can only see the public headers and .a file, but setting the headers "private" when packaging the library didn't work. Adding only the public headers and source files(.a) to the customer's project would cause errors:the “private” headers not found...Any help please?
Without anything concrete to go on, I can describe what I have run into in the past. Your .h files are possibly using #import with references to your private headers. You will need to move those to your .m files. If this causes issues in your .h files due to undeclared classes, etc., you will have to do forward declarations with #class or other similar techniques.

xcode create constant/variable as a build setting?

I have a URL used multiple times in my code and would like to centralize it into something like a build setting constant/variable. How would I go about accessing a build setting from my code? And is this the right thing to do?
Thank you.
Constants.h
static NSString * const myStackURL = #"http://stackoverflow.com/users";
or
#define myStackURL #"http://stackoverflow.com/users"
What you want to do, essentially, is import a header that defines a constant into every one of your other files. The easiest way to do this is to stick it in (application name)-Prefix.pch in the Supporting Files group in the project navigator. Anything defined in this precompiled header can be used by any other file. From Programming iOS 5 by Matt Neuburg:
The precompiled header is a device for making compilation go faster.
It’s a header file; it is compiled once (or at least, very
infrequently) and the results are cached (off in /var/folders/) and
are implicitly imported by all your code files. So the precompiled
header should consist primarily of #import directives for headers that
never change (such as the built-in Cocoa headers); it is also a
reasonable place to put #defines that will never change and that are
to be shared by all your code.

How to invoked methods of external .h files in iphone

to all
In my iphone game I want to add external .h and .a files. I am adding them by using add project option given in Xcode. It get added but I want to invoked the methods given in the header files. As per instruction given to us we have to call one method which is declared in external header file. We have to call that method inside AppDelegate's applicationDidFinishLaunching method . But at the time of compilation it gives one error that is referenced from: and symbol not found. I don't understand how to invoked this method and how to get referenced for the method.
I am sending one link please see that. The same problem with my application is coming.
http://forums.macrumors.com/showthread.php?t=443437
In that three screen shots are given by some toddburch on Jan 24, 2009, 07:37 AM
The same error for my methods are also coming but my code is in Cocoa with objective-c not in ruby.
If anyone know the solution please reply me As soon as possible.
The .h is not enough. You also need the implementation, either source code or compiled framework.
Do you have access to the source code itself? Try dragging the .h and .m/.c files from the framework directory directly into your project. It's a little messy, but I think it will solve your problem based on the error messages you are getting. You can put them all in a group so your project doesn't look so crowded after you drag them over if you like.
You currently have two files
A *.h header file which describes the classes and functions available in your third party library
A *.a static library file which holds the pre-compiled code for the functions described by the *.h file.
Assuming you've got to the stage where you can #include or #import "xyz.h" and compile your application, the bit which is missing is linking your executable with the *.a file.
The symbol not found error message is the linker telling you it knows some of your code is calling a particular function, but it can't currently find another code module that provides an implementation of that function.
One thing to check is that your *.a file is correctly configured within your project to be passed to the linker. One way to do this is to expand the "Targets" section of the main XCode window. If you drill down into the section representing your application you should see a subnode labeled "Link Binaries with Libraries". Your *.a file should be listed, if it isn't one way to add it is to simple drag and drop the file into this section.