Warpzit
Warpzit

Reputation: 28162

How does imports work in swift?

I frankly have a hard time understanding how imports work in Swift. When I make a new class it will start out with:

import foundation

As an alternative we could use import Swift or import UIKit depending on what libraries we need. BUT I've noticed that if I simply remove the imports my projects runs without any problems (even though I use classes from these libraries). This is where I need some help: I'm wondering if that is because I have internal frameworks that I import Swift/UIKit/Foundation and thereby get the import. So imports works like plague... if they touch a new class everything that class touches will have access to that import.

Upvotes: 3

Views: 785

Answers (2)

Warpzit
Warpzit

Reputation: 28162

So we found the issue. Someone had added header file to each framework with:

#import <UIKit/UIKit.h>

Removing this line made everything less mad.

Now about how imports works. If a class A in library A import library B, class A will not have access to UIKit even though library B imports UIKit. This is how I'd expect it to work and how it actually works.

Upvotes: 0

Papershine
Papershine

Reputation: 5223

Yes, when a class that you use imports that framework, it is imported in your class too. This is to make things more clear, for example when class Foo's method abc has a parameter that needs UIKit, which is present in Foundation. Therefore, when you use the class Foo, UIKit is automatically imported.

As a side note, importing UIKit will import Foundation, in which it will also import Darwin. So it is really like plague. If a third party library (such as Charts) imports UIKit, it imports Foundation and Darwin too.

Upvotes: 2

Related Questions