XCode 13.1:未定义的符号:__swift_FORCE_LOAD_$_XCTestSwiftSupport

ela*_*a16 20 xcode ios swift swiftui

昨天我的应用程序运行完美,没有任何错误或警告。但是当我今天尝试运行它时,构建失败了 - 我没有更改代码中的任何内容:

Undefined symbol: __swift_FORCE_LOAD_$_XCTestSwiftSupport
Run Code Online (Sandbox Code Playgroud)

怎么解决这个问题,为什么之前运行的时候会突然出现这样的情况?

这是我最近在使用 XCode 时经常遇到的一个问题。我的代码将顺利运行,不会出现任何错误,但是当我在不同时间重新启动 XCode 时,它​​会随机开始抛出错误 - 而无需对实际代码进行任何更改。

我附上了一些照片和这些照片的全文:

InventoryApp 3 issues
    Warning
        Could not find or use auto-linked library 'XCTestSwiftSupport'
        Could not find or use auto linked framework 'XCTest'
    Error
        Undefined symbol: __swift FORCE LOAD $ XCTestSwiftSupport
Run Code Online (Sandbox Code Playgroud)

照片

ld: warning: Could not find or use auto-linked library 'XCTestSwiftSupport'
ld: warning: Could not find or use auto-linked framework
'XCTest'
Undefined symbols for architecture arm64:
    "__swift_FORCE_LOAD_$_XCTestSwiftSupport", referenced from:
        __swift_FORCE_LOAD_$_XCTestSwiftSupport_$_inventoryApp in Button.o
        __swift_FORCE_LOAD_$_XCTestSwiftSupport_$_inventoryApp in ViewModel.o
        __swift_FORCE_LOAD_$_XCTestSwiftSupport_$_inventoryApp in Inventory.o
        __swift_FORCE_LOAD_$_XCTestSwiftSupport_$_inventoryApp in AddView.o
        __swift_FORCE_LOAD_$_XCTestSwiftSupport_$_inventoryApp in ScannerView.o
        __swift_FORCE_LOAD_$_XCTestSwiftSupport_$_inventoryApp in RegisterView.o
        __swift_FORCE_LOAD_$_XCTestSwiftSupport_$_inventoryApp in ContentView.o
        ...
       (maybe you meant: __swift_FORCE_LOAD_$_XCTestSwiftSupport_$_inventoryApp)
ld: symbol(s) not found for architecture arm64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
Run Code Online (Sandbox Code Playgroud)

照片

Cli*_*iff 38

如果您添加,也会出现这种情况

import XCTest
Run Code Online (Sandbox Code Playgroud)

到非测试包中的文件。

  • 天哪,谢谢你。由于最新的Xcode会自动添加导入语句;我不小心在我的非测试文件中创建了一个 XCTestCase,它在我没有意识到的情况下添加了导入。 (7认同)
  • 是的,使用 Xcode 13 自动导入很容易发生这种情况。如果您输入错误,它可能会导入一个框架,例如 XCTest,而您可能不知道。这就是发生在我身上的事。 (2认同)

ela*_*a16 21

我通过导航到目标应用程序设置>构建阶段>链接二进制与库>添加链接库“XCTest.framework”解决了这个错误。

如果之后您的应用程序开始崩溃,则会中止并出现以下错误:

dyld: Library not loaded: @rpath/XCTest.framework/Versions/A/XCTest

0_abort_with_payload
Run Code Online (Sandbox Code Playgroud)

然后您可以查看此线程上的建议:Xcode 5.0.2 dyld: Library not returned: @rpath/XCTest.framework/Versions/A/XCTest

对我有用的解决方案是:导航到目标应用程序>构建设置>链接>其他链接器标志>编辑或添加-weak_framework“XCTest”


BRO*_*0UL 16

今天我的 Xcode 项目中随机开始出现此错误。我改变了设置。

PROJECT -> Build Settings -> Build Options -> Enable Testing Search Paths
Run Code Online (Sandbox Code Playgroud)

从否到是,构建成功。

编辑:...

如果项目是使用包含测试目标创建的,则默认情况下此设置处于启用状态。就我而言,我相信我稍后添加了测试目标,因此此设置将是错误的。虽然,我不能说为什么我添加测试目标后它没有崩溃。

请参阅: https: //xcodebuildsettings.com