Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is an alternative to pch in swift?

Tags:

xcode

ios

swift

I was wondering what could be used instead of pch in swift.

Is there any alternative to pch or way to get rid of import in swift so that we don't need to do this for all classes.

I don't want to carry around/import all the time. What could be the best substitute of pch in swift.

like image 237
AiOsN Avatar asked Jul 27 '15 08:07

AiOsN


People also ask

What is pch file in Swift?

pch is a precompiled header. Precompiled headers were invented to make compiling faster. Rather than parsing the same header files over and over, these files get parsed once, ahead of time.

How do I create a pch file in Xcode?

From your Xcode menu, select File > New > File... From iOS template options, select Other > PCH file. Name the file <target_name>-Prefix. pch, and then select Create.


2 Answers

You cannot define "Macros" in swift, so there is no ".pch" file for swift.

Also, Swift doesn't have separate "Header (.h) and Implementation (.m)", so there is no need of predefined headers which are needed to compile.

For more information refer to this question, Why .pch file not available in swift?

Class Import Problem:

In Swift, you don't need to write "import" statement for your project classes everywhere. You can access the project classes just by creating their objects.

Pods:

If you are using pods then it's necessary to import frameworks in every class where you are using.

Third Party Swift Frameworks:

For external Frameworks/Modules, you can create a bridging header file and replace the below line with your framework/module name:

@import ModuleName;

Reference: Implicitly import specific Swift module

like image 132
Sohil R. Memon Avatar answered Oct 11 '22 14:10

Sohil R. Memon


There is no Swift equivalent for Objective-C .pch files.

Module is alternative for it.

like image 28
Nilesh Patel Avatar answered Oct 11 '22 13:10

Nilesh Patel