Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift : import UIKit in each subclass?

In objective-C we can do like this:

a. Importing a file in super class

#import "MyAwesomeClass.h"

@interface MySuperViewController : UIViewController
@end

@implementation MySuperViewController
- (void)viewDidLoad {
  [super viewDidLoad];

  //MyAwesomeClass allocated, initialized, used
  MyAwesomeClass *awesomeClass = [MyAwesomeClass new];
}
@end

b. Using the file imported in superclass, in subclass without re-importing it

@interface MySubViewController : MySuperViewController
@end

@implementation MySubViewController
- (void)viewDidLoad {
  [super viewDidLoad];

  //No compilation error, since MyAwesomeClass already imported in superclass
  MyAwesomeClass *awesomeClass = [MyAwesomeClass new];
}
@end

Trying to do the same thing in swift gives compilation error:

a. importing UIKit in MySuperViewController

import UIKit
class MySuperViewController : UIViewController {
   @IBOutlet weak var enterPrice: UITextField!
}

b. Declaring and using an object of UITextField without importing UIKit in MySubViewController

class MySubViewController: MySuperViewController {
    // compilation error at below line
    @IBOutlet weak var myButton: UIButton!
}

Is there any way we can avoid re-importing UIKit in above scenario? Please suggest.

like image 249
Devarshi Avatar asked Apr 19 '15 12:04

Devarshi


2 Answers

Short answer:

Yes. It's my understanding that you need to import all the frameworks you need in each Swift file in your project (it is a file-by-file requirement, not class by class. If you define 2 classes in a single file, you only need one import at the top of the file.)

The #import/#include statements in C are preprocessor directives. It is as if the code in the included file is copy/pasted at the location of the include. If you include a header in your superclass's header, the superclass's header now contains the expanded contents. So when you include the superclass header in your subclass, the system framework headers are included as part of the superclass header.

Swift works a little differently.

like image 110
Duncan C Avatar answered Jan 03 '23 09:01

Duncan C


If you use any objective-C class in your Swift project and import UIKit in that class, you don't actually have to use the import UIKit directive anywhere else in your project!

So basically:

  1. Drag and drop your objective-C .m and/or .h file into your project. Choose copy files if necessary. Make sure all the dependencies are sorted for that file.
  2. (optional) Xcode should prompt you to create a bridging header, if it does, move on to step 4... But if it doesn't, you have to add a header file and call it YourProjectName-Bridging-Header.h.
  3. (optional) If you manually added it, go into Project Settings and search for Swift Compiler. In the General section, there is a place to put the path to the file you just created.
  4. In the bridging header import the objective-c class(es) to your project by doing #import "MyClass.h"

That's it! You are now free to delete your import UIKit statements from every file. It's worth noting that stylistically and for code reuse purposes, it's better to have all of the imports in every file so everyone can see at a glance what dependencies there are, but when you are creating ViewControllers and such I think it's kind of silly to have to import UIKit in every single file. Everyone knows it has a dependency on UIKit and chances are you won't be reusing the UI in another project anyway.

like image 20
James Mundie Avatar answered Jan 03 '23 09:01

James Mundie