开发者

Importing into .h versus .m

Is there a difference between impor开发者_C百科ting something (e.g. #import "JSON.h") into the header file versus the implementation file?


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.


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'.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜