James Skidmore
James Skidmore

Reputation: 50328

Importing into .h versus .m

Is there a difference between importing something (e.g. #import "JSON.h") into the header file versus the implementation file?

Upvotes: 1

Views: 116

Answers (2)

Kristopher Johnson
Kristopher Johnson

Reputation: 82545

If you #import it in the header, then everything including that header gets it. You may find that useful, in that you don't have to #import it again in other places, but my preference is to #import things only where necessary, to minimize dependencies and make builds faster.

Upvotes: 4

Jorge Israel Peña
Jorge Israel Peña

Reputation: 38626

I think if you do it in the header file, you save your self some trouble later on in case you reference a class which is defined in the imported file.

In other words, if you import "JSON.h" in the header file, and there's a JSON class (hypothetically) that you will use in your header file (in the interface), then it would save you from having to do the @class directive at the top. Then your implementation file will also be fine since it would import the header file, which itself imported the "JSON.h" file

Basically I think it would be neater and would be more like objective-c if you import the required files in the interface file (.h). As you've probably noticed, interface files are usually short and concise, allowing you to get a quick glance at what a certain class is about and what it does. If you import your files there, you can also see what files/classes it relies on more easily, saving the implementation file (.m) for the actual 'meat'.

Upvotes: 1

Related Questions