Class name matches namespace name

I have an interesting problem with two different Cocoapods that have a public listing with the same name.

An implicit namespace is usually not a problem, except that both Cocoapods have a class that matches their target name.

So, if I import both Cocoapods into the same file referencing an enumeration with the same name, it generates an "enumeration name ambiguously for type search in this context", and if I try to reference an enumeration using ModuleName.enum Swift, ModuleName does not have a member named enum.

Presumably this is because the class, and not the namespace, does not have a member named enum. Does anyone know about this?

Here's what it looks like in the code:

Cocoapod A:

public enum Test { } public class A { } 

Cocoapod B:

 public enum Test { } public class B { } 

Another file:

 import A import B // Results in "A does not have a member named Test" var test: A.Test = A.Test(rawValue: "a") // Results in "Test is ambiguous for type lookup in this context" var test: Test = Test(rawValue: "a") 
+5
source share
3 answers

There is a simple workaround there, although I'm not sure why Swift doesn't like the Namespace.ClassName syntax.

Create a new file and add this code:

 import A typealias ATest = Test 

Then in your file you can:

 import B var testA: ATest = ATest(rawValue: "a") var testB: Test = Test(rawValue: "a") 

Alternatively, you can also create an equivalent toalias file for BTest.

+1
source

In Swift 4.x and Xcode 10, typealias solution typealias not work.

Instead, you can use Module.Class , in your case just use A.Test or B.Test .

0
source

You need to import a specific type in order to transfer it to the namespace of your files in front of others, such as named types.

I will use the SwiftMessages framework as an example, there enum Theme conflicts with a similar named class in another framework.

SwiftMessages also exists as a class inside the framework, and Swift always thinks that I want to access the class when I am SwiftMessages.Theme .

 import enum SwiftMessages.Theme 

Now you can use Theme

 var theme: Theme 

and that will be the expected topic.

If you still have conflicts, create a separate source file

 // eg SwiftMessages+Support.swift import enum SwiftMessages.Theme typealias SwiftMessagesTheme = SwiftMessages.Theme 

And then use it elsewhere:

 var theme: SwiftMessagesTheme 
0
source

All Articles