"unsafeMutableAddressor : Swift.String",引用自:.."

"unsafeMutableAddressor : Swift.String", referenced from:.."

我收到一个错误:

Undefined symbols for architecture x86_64: "DirectBistro.DBTabBarOrderedIndexesKey.unsafeMutableAddressor : Swift.String", referenced from: DirectBistroUITests.TabBarControllerTests.setUp (DirectBistroUITests.TabBarControllerTests)() -> () in TabBarControllerTests.o ld: symbol(s) not found for architecture x86_64

这是我的简单UITestclass:

import XCTest
@testable import DirectBistro

class TabBarControllerTests: XCTestCase {

    override func setUp() {
        super.setUp()

        let defaults = NSUserDefaults.standardUserDefaults()
        defaults.setObject([], forKey: DBTabBarOrderedIndexesKey)
        defaults.synchronize()
    }
}

DBTabBarController.swift中是这样定义的:

let DBTabBarOrderedIndexesKey = "TabBarOrderedIndexesKey"

信息 窗格:

常规 窗格:

结论是:行不通

我将其作为错误报告给 Apple,并得到回复:

UI tests execute differently from Unit tests - Unit tests run inside your application process so they can access your application code. UI tests execute in a separate process, outside your application, so they can simulate how the user interacts with the application. It’s not expected that you will be able to access your app class from a UI test.

可以从您的 UI 测试访问应用程序代码。只需将源文件添加到 UI 测试目标:

然后您可以从 UI 测试代码中访问该应用程序代码:

但请记住,您从 UI 测试中访问的应用程序代码是 运行 在您的 UI 测试目标中的代码 (MyAppUITests),它与实际应用程序目标(MyApp)中的代码运行不对应。所以不要用它来检查或修改应用程序状态。

我在添加 swift 包并尝试在其中使用字符串变量时遇到了同样的问题。修复只是在目标常规部分的框架、库部分添加 swift 包

我在 运行 测试用例时收到类似的错误。我发现在我的测试用例中从主 iOS 应用程序项目的另一个文件中引用了某些字符串值。在这里无法访问它们。 我通过在我的测试用例方法中创建一个本地常量来解决它。您可能会建议一个更好的方法。