I recently created a new iOS project in Xcode 13.3 and stumbled into a really annoying error which is not yet documented, but easy to fix.
So let's look into how we can reproduce this error for a moment:
1. Create a new iOS Multiplatform project in XCode.
2. Give it any suitable name and include test cases from the creation wizard.
3. Create a new Swift file and call it MagicModel.swift.
4. Add some simple stub code like:
struct MagicModel {
var title: String
}
5. Do not add MagicModel.swift to the test target, we are going to use @testable import instead.
5. Go to the automatically created test case file Tests_iOS.swift.
6. Add a trivial test case like:
func testMagicModel() throws {
let magicModel = MagicModel(title: "The model")
}
and add @testable import YourAppName after the import XCTest on the top of the file.
Next up, when you try to run the tests you will get a compiler error message:
Undefined symbol: YourAppName.MagicModel.init(title: Swift.String) -> YourAppName.MagicModel
Luckily, you don't need to panic, because the cause of this issue and the solution are really simple:
The default unit test type in that file producing the compiler error is a UI test instead of a normal unit test.
UI tests do not support
@testable import.
So the solution is to add that test you were working on to a Unit Test target instead of the default UI Test target.
In case you do not have a Unit Test target yet, create one by:
If you are lazy, clone my project from github:
https://github.com/rph8/UndefinedSymbolApp
It shows the solution, i.e. how it works.
If you uncomment the lines in Tests_iOS, you can see how it does not work.
This is where you would typically go wrong.