开发者

Should I import UIKit in every file I code?

I'm coding for the iPhone and wondering if I need to include UIKit in every file that code or is there a better rule as to when开发者_JAVA技巧 to do so and when you don't have to?

Thanks!


While the pch file is handy for pre-compiling headers for you, I really recommend that files import what they use. That makes it much easier to understand the dependencies, and much easier to reuse the code, including reuse across platforms (iPhone vs. Mac).

With that in mind, the answer is that you shouldn't import UIKit.h into every file. You should import it into every file that uses UIKit. That should be your View and Controller classes. Model classes should almost never use UIKit. They should generally import Foundation.h. Following this rule will make it easy to move your objects into other projects which may have different code in the .pch file, and easier to reuse your model classes on both iPhone and Mac.

I also typically do not recommend that you import UIKit.h or Foundation.h into subclasses of your own classes. I typically just import UIKit, AppKit or Foundation into the highest-level header file in my code. For example, if I have a UIView subclass called MYAbstractView that imports UIKit.h (to include the definition of UIView), and then add a MYConcreteView subclass, I would just import MYAbstractView.h.

Note that UIKit and AppKit both import Foundation already, so there's no reason ever to import both.


You must use the UIKit when you need GUI components. But you don't need to import this library if it's already define on the Prefix.pch:

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

It allows you to import these libraries for all your current project.


You should import it in the .h file of any class that needs it. This is true unless the class is extending another class that imports it, then you do not need to. You probably do not want to do anything more complex since all iPhone apps will be linking the library. If we were talking about a 3rd party or less common library then you might want to be careful about it. To avoid code reusability issues.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜