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

joseph.hainline picture joseph.hainline · Jun 4, 2014 · Viewed 169.2k times · Source

I simply want to include my Swift class from another file, like its test

PrimeNumberModel.swift

import Foundation

class PrimeNumberModel { }

PrimeNumberModelTests.swift

import XCTest
import PrimeNumberModel  // gives me "No such module 'PrimeNumberModel'"

class PrimeNumberModelTests: XCTestCase {
    let testObject = PrimeNumberModel()  // "Use of unresolved identifier 'PrimeNumberModel'"    
}

Both swift files are in the same directory.

Answer

H6. picture H6. · Jun 4, 2014

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.