XCode importing a file to whole project

Hi,

Sometimes you may encounter situations where you have a file which is used frequently throughout the XCode project. This may be a configurations file which is used in 80% of your code files. Importing this in each file is a certain possibility(and the very normal way with no issues), but if you are lazy and just want to add it in one place, this is the post for you.

If you analyze the project, there is a file named <project-name>.pch. There is a block of code which has the following.

#ifdef __OBJC__
    #import <UIKit/UIKit.h>
    #import <Foundation/Foundation.h>
#endif

NOTE: As you can see, UIKit is already added and so is the Foundation framework. That is why you can use classes such as "UIView" or "NSArray" without any additional imports in your code file. That's just an extra bit of info for the curious !!!.

So, likewise, we can add our commonly used file into that block as well in the following manner.


#ifdef __OBJC__
    #import <UIKit/UIKit.h>
    #import <Foundation/Foundation.h>
    #import "MyConfigFileName.h"
#endif    


Once you add this, you can use "MyConfigFile" within your project without additionally importing it in each file.

NOTE: Remember, this is not the place to import each and every file for convenience. Only do it if the file is needed to be imported across a wide number of places in the project. Otherwise it'll just bring more trouble.

Happy coding :)

Comments

Popular posts from this blog

Encrypt and Decrypt Images Using Java

Build your own Network sniffer

ASP Response.Write newline