Reputation: 3088
I have Cocoa Touch Framework. Each of all three following code samples as source of my Framework is successfully compilable.
import Foundation
protocol X {
var a: Data { get }
}
protocol X {
var a: Foundation.Data { get }
}
protocol X {
var a: Data { get }
}
The last one uses type from Foundation without any importing of this module. Is Foundation imported by default into Swift file in case when module header (.h file) contains #import <UIKit/UIKit.h>
(generated by default)?
Note (30.10.19)
I removed #import <UIKit/UIKit.h>
and replaced FOUNDATION_EXPORT
with extern
in module header (.h file). After that I added import Foundation
into my swift file. As a result only first example of code is compilable.
As I understand if module was imported into header (.h) of another module - it isn't necessary to import first module into Swift files of second module. Am I right?
Upvotes: 1
Views: 840
Reputation: 1348
Yes UIKit
imports Foundation
so if you have #import <UIKit/UIKit.h>
or import UIKit
there is no need to import Foundation
Take a look at here in UIKit
's source swift code Here
Edit
Here is useful information about how modules work in swift,
You can also selectively import certain declarations from a module:
import func Chess.createGreedyPlayer import class Foundation.NSRegularExpression
Comparison with Other Languages
Importing a module is much like importing a library in Ruby, Python, or Perl, importing a class in Java, or including a header file in a C-family language. However, unlike C, module files are not textually included and must be valid programs on their own, and may not be in a textual format at all. Unlike Java, declarations in a module are not visible at all until imported. And unlike the dynamic languages mentioned, importing a module cannot automatically cause any code to be run.
Importing a module in swift uses one of three syntaxes below:
import [module]
import [module].[submodule]
import [import kind] [module].[symbol name]
when we do not specify the import kind while importing Data
, we’re actually still just importing the entirety of Foundation
.
there are multiple permissible import kinds available:
typealias | struct | class | enum | protocol | let | var | func
Like any other body of code, a module may depend on other modules in its implementation. The module implementer may also choose to re-export these modules, meaning that anyone who imports the first module will also have access to the declarations in the re-exported modules.
@exported import AmericanCheckers
As an example, the "Cocoa" framework on OS X exists only to re-export three other frameworks: AppKit, Foundation, and CoreData.
Just as certain declarations can be selectively imported from a module, so too can they be selectively re-exported, using the same syntax:
@exported import class AmericanCheckers.Board
Upvotes: 3