JManke
JManke

Reputation: 593

Widget Extension UnitTest Error: "Undefined Symbol: nominal type descriptor for [...]."

I have a Widget Extension for my app with a struct MyStruct defined in my extension, with target membership MyAppWidgetExtension. I also have a UnitTesting target, in which I want to have UnitTests for my main App as well as for the WidgetExtension.

As soon as I add a file to the UnitTesting-target, which has a var/let of type MyStruct, e.g. var foo: MyStruct? (which is recognized by the compiler by @testable import MyAppWidgetExtension within this file), executing the UnitTests yields a compiler error: "Undefines symbol: nominal type descriptor for MyAppWidgetExtension.MyStruct".

Any idea what's going wrong here?

Upvotes: 4

Views: 2528

Answers (2)

Mojtaba Hosseini
Mojtaba Hosseini

Reputation: 119868

This usually happens when Xcode can see the package but somehow the compiler can not!

So make sure:

  1. The package you are trying to use is LINKED as a dependency of the active target. Note that it is different than the package dependencies.
  2. The package is imported into the file you are trying to use.
  3. The code you are using from the package is visible from the outside. (at least public or @testable in test targets)

SPM Demo

let package = Package(
    name: "MyTarget",
    products: [
        .library(name: "MyTarget", targets: ["MyTarget"]),
    ],
    dependencies: [
        .package(url: "https://github.com/firebase/firebase-ios-sdk", from: "10.27.0"), // 👈 0. Make sure it is added as a dependency of the PACKAGE
    ],
    targets: [
        .target(
            name: "MyTarget",
            dependencies: [
                .product(name: "FirebaseAnalytics", package: "firebase-ios-sdk"), // 👈 1. Make sure it is LINKED as a dependency of the TARGET
            ]
        ),
    ]
)

Xcode Project Demo

The steps are the same as the SPM but you should use the Xcode GUI,

The most missing step is the 1st step where you forget to link the imported framework to the active from the General tab -> Frameworks, Libraries and Embedded Content section.

Upvotes: 0

JManke
JManke

Reputation: 593

I've found a solution, which feels like a workaround, but does the job:

  • Define MyStruct as well in the main app target with target membership MyApp
  • In the UnitTest use @testable import MyApp in order to be able to use MyStruct from the app target

Now the compiler does not complain anymore.

Upvotes: 4

Related Questions