Cazz Brunestud Cazz Brunestud - 17 days ago 5
iOS Question

Swift - About importing packages vs classes

I need to use MKMapViewDelegate in this class of mine, so I was wondering if it is a good practice to import

import MapKit.MKMapView


instead

import MapKit


Does importing whole package hit app performance or is there anything I should take into consideration in terms of performance?

Answer

import MapKit.MKMapView makes no practical difference. It still imports all of MapKit (try creating an MKShape() for instance or any other MapKit class). Swift doesn't support importing individual classes this way. I don't know why it even allows the syntax (it may be worth opening a bug about that because it's confusing).

There are a small number of cases cases where there are actually sub-modules, and then you have to import the submodule directly (importing the umbrella isn't always good enough). For example, to get some category methods from SceneKit, you have to import SceneKit.ModelIO explicitly (but I expect this isn't really intended behavior, either).

But in any case, there isn't today a good reason to restrict your imports. Eventually, hopefully, Swift will get better importing capabilities that let us control our namespaces better, but for now, just import the module.