Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should I import UIKit in every file I code?

Tags:

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 to do so and when you don't have to?

Thanks!

like image 485
Anthony Glyadchenko Avatar asked Feb 17 '10 17:02

Anthony Glyadchenko


People also ask

What is import UIKit?

UIKit is the iOS framework that implements the standard UI components for iOS applications. Building an app with UIKit is trivially easy. In all cases you'll use Xcode, Apple's IDE for developing for iOS and macOS. In Swift, you just put the statement import UIKit.

How do I add UIKit framework to Xcode project?

Navigate to the target and go to Build Phases and add UIKit to Link Binary With Libraries . From the Comments to this post: MonkeyBusiness: Sorry to waste your time. I had set the project up as MacOS rather than iOS.

What is UIKit and foundation in Swift?

UIKit is an "umbrella" framework, which imports a lot of other frameworks, including Foundation, so it really is imported. There's actually more to this than meets the eye. As it happens, Swift contains a re-implementation of the NSString APIs within the String class.


1 Answers

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.

like image 180
Rob Napier Avatar answered Sep 19 '22 06:09

Rob Napier