Bartłomiej Semańczyk
Bartłomiej Semańczyk

Reputation: 61880

Why MapKit framework must be linked to every file while CoreData framework must not?

Linked frameworks and libraries in my project written in Swift:

enter image description here

Suppose I have two files in my project: AppDelegate.swift and ViewController.swift

My import statements in AppDelegate.swift:

import UIKit
import Foundation
import CoreData
import MapKit

So simply here I can use for example: NSManagedObject and MKMapView classes.

In ViewController.swift I do not have any import statements, but I can use there NSManagedObject opposite to MKMapView. Why?

Shouldn't be in Swift that one framework imported in one file should be visible to other files? Why CoreData is visible to all, but MapKit is not?

I also use Project-Bridging-Header.h in my project with following import statements:

@import AFNetworking;
@import Crashlytics;
@import Fabric;
@import FBSDKCoreKit;
@import FBSDKLoginKit;
@import MagicalRecord;
@import MTBBarcodeScanner;
@import QRCode;
@import SVProgressHUD;

Upvotes: 2

Views: 78

Answers (1)

Bartłomiej Semańczyk
Bartłomiej Semańczyk

Reputation: 61880

  • Swift works with modules.
  • Every file is visible to other files in the same module without any imports.
  • Every class created in different module can be accessed outside only via import statement. Frameworks are different modules. If you need to access framework you need to import it for every file where you gonna use it.

  • Bridging-Header.h file distributes other modules/frameworks to every file in module where such header exists.

  • if module A import module B, and you need to access both of them in module C, it is enough to import just module A in your C module.

Upvotes: 1

Related Questions