如何从另一个 Swift 文件导入 Swift 文件? [英] How do I import a Swift file from another Swift file?

查看:57
本文介绍了如何从另一个 Swift 文件导入 Swift 文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是想从另一个文件中包含我的 Swift 类,比如它的测试

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'"    
}

两个 swift 文件都在同一个目录中.

Both swift files are in the same directory.

推荐答案

我遇到了同样的问题,也在我的 XCTestCase 文件中,但在常规项目文件中没有.

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

摆脱:

使用未解析的标识符PrimeNumberModel"

Use of unresolved identifier 'PrimeNumberModel'

我需要在测试文件中导入基本模块.就我而言,我的 目标 被称为myproject",我添加了 import myproject 并且该类被识别.

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.

这篇关于如何从另一个 Swift 文件导入 Swift 文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆