Swift Namespace Conflict

Swift namespace conflict

This appears to be a Swift bug. As a workaround, you can create a new Swift file in the app that imports only FrameworkA and defines a typealias for Thing:

import FrameworkA

typealias ThingA = Thing

Then in the file that needs to import both frameworks, you use ThingA instead of FrameworkA.Thing.

Namespace conflict on bridging header and framework with swift and objective-c

Try namespacing the User class. It would look like this:

// use the class from different frameworks
let user1 = Framework1.User()
let user2 = Framework2.User()

// use the class from the main project
let user4 = User()
let user3 = MyProject.User() // also works like this, if you want to be explicit. Here MyProject is the product module name

Try with the fourth option.

Otherwise the easiest way would be renaming your objective-c User class.

How to resolve conflict between struct and module names?

The following scenario resolves name conflict between struct and module for second case which contains A, B, C projects. This solution is based on rule that we can edit only C project. Because C is our client of hypothetically third-party unchangeable modules A and B.

  1. Add new Swift file into C project with code below
import struct A.B
typealias StructB = B

  1. Change code in Src.swift to the following
import B
let structB: StructB? = nil

struct X { }
typealias Y = B.X

Result is succeeded compilation without error:

'X' is not a member type of 'B'

And we can use struct B from module A and struct from module B in single file. The name conflict has been resolved.


The second one scenario resolves conflict in the first case with Data.

  1. Replace
import Foundation

with

import struct Foundation.Date
import struct Foundation.UUID

  1. Be sure that Foundation isn't imported in module level. In my case I removed #import <UIKit/UIKit.h> from Data.h. And replaced FOUNDATION_EXPORT with extern to do Data.h compilable.

Naming conflict between two classes from my project and a pod

First, your extra question…

Swift namespaces are by module. Once you bring the name Product in from Tracker in one file the name exists in the whole module. (Though for some reason you still have to import external modules into each file that you want to actually use the module in, I have no idea why that is…)

Now for your issue of the two Product names colliding…

The tradition with Objective-C classes is to put a three letter prefix on them. This has always been done because there is only one namespace in Objective-C. With only one namespace the odds that there will be multiple version of Product becomes extremely high. Two letter prefixes are reserved by Apple which is why you see things like NSString, UIViewController, etc. Apple uses "NS" or an abbreviation from the framework's name for all of their prefixes. Individual developers and companies have traditionally used their own initials, company initials, or in the case of frameworks and libraries an abbreviation of the corresponding name.

Since the class you are having collision issues with is an Objective-C class the easiest thing to do is follow tradition and stick a prefix on it. It isn't very Swift-like but it isn't a Swift class, it's an Objective-C class…

Namespace in Swift: Differentiate between an enum and a class

It turns out you can make a file that has a typealias

import class Fruits.Banana

typealias BananaClass = Fruits.Banana

Then just use BananaClass instead in ClassA



Related Topics



Leave a reply



Submit