How to Import a Swift File from Another Swift File

How do I import a Swift file from another Swift file?

I had the same problem, also in my XCTestCase files, but not in the regular project files.

To get rid of the:

Use of unresolved identifier 'PrimeNumberModel'

I needed to import the base module in the test file. In my case, my target is called 'myproject' and I added import myproject and the class was recognised.

How do I import other Swift files into a Swift script?

There's currently no way to import other swift files in a interpreted swift script. But you can concatenate all source files before executing them:

cat one.swift two.swift three.swift | swift -

If you're using the swift compiler, just add the files you want to compile together before the -o argument:

swiftc one.swift two.swift three.swift -o combined

No need to import them, they are already in the same module.

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()

Import swift iOS project to another project

See this example : -

http://tuchangwei.github.io/2013/09/16/how-to-import-one-project-to-another-one-in-xcode/

Due to user needs, I have to import one project to another one as a sub-project. The final result should be like the following picture.

It is not easy to do this. We need some steps.

Step 1, Add “project.xcodeproj” file to project, note that don’t choose the “Copy items into…”.

Step 2, close the project, because you have opened the project in project. If you don’t, you can just see the “project.xcodeproj” file and there is no triangle front of it, you will can’t expand it.

Step 3, generate static library. Choose “project.xcodeproj”,add Target. After generating static library, add swift file framework files to it.

Step 4, generate Bundle, and change the relevant settings to iOS target.

Passing variables from one swift file to another

If vars.swift a file from where you will access constants you can create the constants in a struct and then access them from the other file

struct Constants {
static let v1 = "var1"
}

file1.swift

class File1 {

func test() {
print( Constants.v1);
}

}


Related Topics



Leave a reply



Submit