1

G'Day Programmers,

Lets get straight onto some of the issues I noticed. I was cleaning up my code and learning how much boilerplate code is being generated by Xcode. I found something unusual, In my prefix file(Assuming all of you must have this too...) I have some code like this,

enter image description here

[Image 1.1]

In Image 1.1 we can clearly see that, If definition is Objective C than #import <UIKit/UIKit.h>. This will save us rewriting #import statement in all our other classes because by definition this import will be available to all Objective C classes in our project.(I might be wrong, Please hit me with the brick if I am wrong! :) ).

However, Whenever you add a classes or when you choose ViewBased application(or whatever...) there will be few classes already created for you with awesome amount of boilerplate code including #<UIKit/UIKit.h> like,

enter image description here

[Image 1.2]

As we can see in Image 1.2 Xcode again added import statement, So my confusion is,

I can't understand

  • Why Xcode is adding #import multiple times?
  • Do we really need to #import twice?
  • Should I worried about this or shut up and start making awesome apps?

Thanks for you input

TeaCupApp
  • 11,316
  • 18
  • 70
  • 150
  • http://stackoverflow.com/questions/439662/what-is-the-difference-between-import-and-include-in-objective-c – vikingosegundo Nov 09 '11 at 23:21
  • I don't know why you shared that link vikingosegundo? My question is about two times #import rather than what is the difference between #import and #include. :) – TeaCupApp Nov 09 '11 at 23:24
  • Read the accepted answer: It doesn't matter, if there are several imports. – vikingosegundo Nov 09 '11 at 23:29
  • @vikingosegundo: The question isn't about the safety of multiple imports but why there *are* multiple imports in the project templates. – dreamlax Nov 10 '11 at 19:49

2 Answers2

1

It's likely included twice so that if you decide to disable the prefix header it won't break your code.

dreamlax
  • 93,976
  • 29
  • 161
  • 209
  • Interesting but question is why only UIKit.h why not Foundation.h? :) As if you disable prefix header your code will broke if you have no Foundation framework. am I right? – TeaCupApp Nov 09 '11 at 23:17
  • Maybe not directly, but UIKit classes utilise Foundation classes so it must be included somewhere. – dreamlax Nov 09 '11 at 23:20
  • Sorry my bad! you are 100% right app is running without prefix foundation import but crashes without uikit. Thanks you very much! – TeaCupApp Nov 09 '11 at 23:37
1

You get multiple imports of the same thing because the Xcode templates are of uneven quality. Some are good, some are not so good. They're written by people, who tend not to be perfect.

You don't really need to worry as #import prevents headers from being included more than once.

You also don't need to #import twice as it's only being included once.

Terry Wilcox
  • 9,010
  • 1
  • 34
  • 36