6

I have an enum in one of my Swift files called Foo. One of the Cocoapods called NameA also has the same enum with name Foo (public enum, not inside any class). This module also has a class with the same name as its framework NameA.

If I try to refer to Foo in NameA module like this:

NameA.Foo

It doesn't work because the compiler thinks I'm referring to the class NameA, but not the module NameA. The workaround posted here wont work for me either Swift namespace conflict

This seems to be a reported bug in swift: https://bugs.swift.org/browse/SR-898

Community
  • 1
  • 1
Tahdiul Haq
  • 245
  • 3
  • 11
  • 3
    I had this problem before: [How can I disambiguate a type and a module with the same name?](http://stackoverflow.com/questions/37892621/how-can-i-disambiguate-a-type-and-a-module-with-the-same-name) – zneak Sep 26 '16 at 19:55
  • In case you need access to both `Foo` enums in the same file, you may have to rename/typealias one or the other. You could typealias `NameA.Foo` to `NameAFoo`, for instance, in a file that only contains `import enum NameA.Foo; typealias NameAFoo = NameA.Foo`. – zneak Sep 26 '16 at 19:59
  • @zneak thanks but your solution does not work for me. Both the "Foos" have access to my entire module (my one one is internal, and ther one under NameA is public) so i cant typealias it from another file. Importing just the enum wount work either. I think i will have to rename my one. – Tahdiul Haq Sep 26 '16 at 20:41

3 Answers3

11
  • Don't import NameA.

  • Instead, import enum NameA.Foo (notice the keyword "enum", can also be used for "struct" or "class" or "func")

  • Reference either Foo (your enum) or NameA.Foo (their enum).

If you need to reference NameA in the same file as NameA.Foo:

  • Create a new file for your "wrapper" type:

import NameA typealias NameAWrapper = NameA

  • Reference the class NameA as NameAWrapper in your other files without importing the module directly.

This was inspired by this workaround (which you linked to), but modified slightly based on your situation: https://stackoverflow.com/a/26774102/358806

Gazzini
  • 728
  • 8
  • 19
0

I ran into a similar issue recently. You won't like the solution I found, but I'll share it anyway. I had to fork the pod I was using and rename it to something new. The new project name no longer conflicted with the class name and I was able to namespace it as MyForkedName.ClassName. This is really an inconvenient way to do it, but in our case it was an older library that hadn't changed in some time (and one we will be removing altogether in the future) so I was willing to settle for now.

Mike Cole
  • 1,291
  • 11
  • 19
0

If you want to use enum from other module like Cocoapods or some framework you have to use this approach,

suppose I have enum Result defined in my project and same in some other module. You want to use both enum in same file then-

import enum MyFramework.Result

func doSomething(callback: (Result<Data>) -> Void) {

}

func doSomething1(callback: (MyFramework.Result<Data>) -> Void) {

}
iKushal
  • 2,689
  • 24
  • 20