Jeef
Jeef

Reputation: 27275

X is not a member type of Y

I'm having staring issues with Module name spacing in a swift project.

I've tried this in a new project and everything works fine:

I have 2 modules with which contain the same class name and i'm able to reference the class from Module.Class no problem.

I have an existing swift project and I'm unable to get this working. I keep getting the error

enter image description here

So as you can see if I don't try to Module-Scope the class everything works fine.

(And yes i've tried with and with out the .Type addition).

So I'm assuming there is something off in my compiler setting. The only other thing I can think of is that my LocationMessage class is defined not in the main DataManager "class" file but rather in a different file.

But I really can't make heads or tails of whats going on. Any suggestions?

Project Structure

Framework: DataManager

Framework: ReferenceTest

So my issue is that in File.swift I'm trying to reference a class defined in LocationMessages.swift inside the DataManager.framework The class IS public

@objc(DMLocationMessage)
final public class LocationMessage : ParsedMessage {

Upvotes: 20

Views: 13237

Answers (6)

Mojtaba Hosseini
Mojtaba Hosseini

Reputation: 119370

There is a workaround for this! 🎉

You should

  1. Create a new empty swift file
  2. Import just the type (not entire module) you need to use:
/* import MyModule */ // NOT THIS!

import enum MyModule.Theme // This will only import the `enum`.  You can use this method for `class`, `structs` and others too.
  1. Make a typealias for the imported type:
typealias MyUniqueName = MyModule.Theme
  1. Use the new unique name that you just created.

This is the only workaround that compiler understand what do you mean exactly (without need to change the internal module names.


Result

Remember the final file will contain only two lines:

import enum MyModule.Theme
typealias MyUniqueName = MyModule.Theme

Upvotes: 0

pikzelz
pikzelz

Reputation: 56

When using an xcframework that depends on another framework, the X is not a member of type Y will also happen for the Framework it depends on when writing an extensions that wraps, for example, delegate methods

Example: Framework Foo depends on Bar and need to conform to DataDelegate.

class Foo {
    ...
}

extension Foo : Bar.DataDelegate {
    ...
}

Everything during compiling will go as planned and the xcframework will be generated. Once you add it to your App along with the dependancy framework Bar and try to build it you will get DataDelegate is not a member of type Bar.

Upvotes: 2

Alpana
Alpana

Reputation: 983

If using xcframework follow below steps to get rid of error :

  1. In terminal, go to the folder where .xcframework is located.
  2. Run the following terminal command after replacing the frameworkName with your framework name:

find . -name "*.swiftinterface" -exec sed -i -e 's/frameworkName\.//g' {} \;

  1. Replace the old xcframework in your project with xcframework generated with above command. Clean build the project.

The above error only occurs in xcframework when a class named is same as the module name. Details on Apple forum at: https://forums.developer.apple.com/thread/123253

Upvotes: 12

Derryl Thomas
Derryl Thomas

Reputation: 1568

In case anyone is here while trying to build and use an xcframework, apparently, the issue is caused when you have a class in the framework which has the same name as the module/framework.

This is a known issue and needs a workaround as mentioned here.

P.S. - I know this doesn't answer this question per se. Just leaving it here for anyone else who might land here because of the question title.

Upvotes: 18

Anton
Anton

Reputation: 736

There is also this bug in swift compiler: SR-631 Extensions in different files do not recognize each other

The result (success/failure) of the compilation depends on the order of files in Build Phase > Compile Sources setting.

I had exactly the same error message: X is not a member type of Y. Solved it by rearranging compilation sources.

Upvotes: 24

fluidsonic
fluidsonic

Reputation: 4676

You likely have a class/struct/enum DataManager anywhere in your code which hides the DataManager module. The error states that it cannot find LocationManager in type DataManager where it should read module instead.

Your app modules should be designed in a way so that you never need to explicitly use the module's name except for the import statement. So just use LocationMessage.Type directly without stating the module.

This is one reason all our app's modules are prefixed with an X, e.g. XDataManager. This avoids conflicts with Apple's modules, external modules (like from CocoaPods) and with types (like in your case).
The X also makes it obvious that these modules are part of the app itself and not some third-party frameworks.

Upvotes: 7

Related Questions