0

I have multiple projects that use my API library. This API use a generic class name like "Document" or "Contract" for his models.

I know that I can use prefix but I ask myself if is not better to do inner classes like this:

//My main class

class MyApi {

    func fetchDocuments(completion: ([Document]) -> Void){}
    func fetchContracts(completion: ([Contract]) -> Void){}
}

//MyApi.Document.swift

extension MyApi {
    class Document { 
      .... 
   }
}

//MyApi.Contract.swift

extension MyApi {
    class Contract { 
      .... 
    }
}

//a cell config
func setup(document: MyApi.Document) {

}

It seems more clear that this object is related to this API and resolve a name conflict with other class with the same name. Do you think that is a good solution? Which alternative if not?

AtulParmar
  • 4,358
  • 1
  • 24
  • 45
Anthony
  • 2,801
  • 3
  • 30
  • 49
  • I like this personally, but there is one caveat: You sometimes have to manually order the build files to ensure `MyApi` is compiled before the rest of the files. I'm not sure in what circumstances it breaks down, but it does sometimes. – Alexander May 20 '19 at 13:57

2 Answers2

4

This is a good practice to make your namespace organized and the question is related to the below one: Swift nested class properties

Abdo
  • 342
  • 1
  • 8
2

There is no problem with inner classes/structs this depends on how you want each module to be separate from the other , the other option is having a long naming convention

class ModuleA  ... }
class ModuleA1 { ... }
class ModuleA2 { ... }

class ModuleB  ... }
class ModuleB1 { ... }
class ModuleB2 { ... }

An advantage with nested is you can apply hiding with just changing access modifier with root class instead of writing it in front of each separate class

Shehata Gamal
  • 98,760
  • 8
  • 65
  • 87