Home > Software design >  Namespaces between Swift Packages
Namespaces between Swift Packages

Time:04-12

I am using two swift packages with SPM. But it appears both are defining a struct with the same name. It compiles, though it seems one struct overrides the other. How can I choose which of the structs I want to use? Is there some namespace for each package?

Thank you..

CodePudding user response:

Let say you have package ‘A’ and ‘B’ and the struct name is ‘SomeModel’ You can simply

import A
import B
let modelA = A.SomeModel(…)
let modelB = B.SomeModel(…)

This is the default behavior/namespacing for different modules/packages in Swift. Though there might be an additional encapsulating/name-spacing within the package. Something like

class SomeClass {
  struct SomeModel{}
}

Then you can access it with the additional encapsulation

  • Related