Swift - Import my swift class Swift - Import my swift class swift swift

Swift - Import my swift class


You don't need to explicitly import files in Swift as they are globally available through the project. If you want to access the methods or properties of Player class, you can directly make object of Player class in MainScene.Swift file and can access to it.e.g var objPlayer = Player()


There is no need to import swift classes to use in other swift classes.They are available to use automatically.

In Swift you can only import module, e.g. any framework like UIKit, MapKit as below. You cannot import swift classes.

import UIKitimport MapKit

Just make sure its selected to use in target in which your are trying to use.

Check below images for more idea.

In this image my HomeViewController.swift is selected to use in AutolayoutDemo module.enter image description here

In below image I have unchecked AutolayoutDemo module for the class DetailsViewController.swift.enter image description here

So now onwards when I will try to use the DetailsViewController compiler will give me error as below image in HomeViewController.enter image description here


When it comes to Swift imports, there are two cases:

1) The type to import is in the module

In this case, no import statement is needed. As long as the type is not private or fileprivate, you can directly access it.

2) The type to import is outside of the module

You can import an entire module using:

import ModuleName

If you only want to import a specific type or function from the module, you can do this using the following format:

import kindOfThing ModuleName.Type

where kindOfThing is class/struct/func/etc...

A much deeper exploration of this can be found on NSHipster here.