Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Namespace Conflict in swift with cocoa pod module

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

like image 951
Tahdiul Haq Avatar asked Sep 26 '16 19:09

Tahdiul Haq


1 Answers

  • 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

like image 70
Gazzini Avatar answered Oct 21 '22 06:10

Gazzini