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.
- Add new Swift file into C project with code below
import struct A.B
typealias StructB = B
- 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.
- Replace
import Foundation
with
import struct Foundation.Date
import struct Foundation.UUID
- Be sure that Foundation isn't imported in module level. In my case I removed
#import <UIKit/UIKit.h>
from Data.h. And replacedFOUNDATION_EXPORT
withextern
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
Swift Nsusernotification Doesn't Show While App Is Active
Swift: Google Maps Draw Waypoint Polyline
Dismiss View from View Model [Modal Page]
Get the String Up to a Specific Character
Swift: How to Create a Predicate with an Int Value
Name Convention for Unwrapped Value in Swift
How to Add Documentation to Enum Associated Values in Swift
Swift Codable with Different Array Types
How to Add Openssl to a Swift Project
"Message from Debugger: Unable to Attach" When Running Tests on Osx App
Swift. Is the (Absolutely) Sole Specific Advantage of Unowned Over Weak, Performance
Swift: Oslog/Os_Log Not Showing Up in Console App
[_Nscftimer Copywithzone:]: Unrecognized Selector Sent to Instance