Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift - Can't use Namespace when Framework/Module with class with same name

Environment:

Swift >=2.3, Swift Framework in a Swift app

SetUp:

  • A Swift dynamic framework with module name, let's say APIService.framework.
  • It has a public class APIService.swift and it has static functions.
  • It has another public class Item.swift.

  • A swift application that usage APIService.framework

  • App can work with the framework like APIService.function()
  • App also has a class Item.swift

If App needs to refer Item class of framework it has to do APIService.Item but since APIService is a class inside the framework, compiler always try to look for a property inside APIService class rather than in the APIService Module, hence throws an error saying

'Item' is not a member type of 'APIService'

Possible Solutions:

  • Change the framework's Module name to something other than class name.
  • Change the class name to something other than the Module Name.
  • Put a "Item" static property inside APIService class that points to
    Item class in framework.

All these are just workarounds, the real issue remains that compiler is not able to differentiate between Module name and class name. Do we have anything in Swift by which I can say "Don't look into the ModuleName.swift, instead look into the whole Module" ?

like image 724
tarun_sharma Avatar asked Dec 24 '16 07:12

tarun_sharma


1 Answers

Based on this answer, it looks like you can directly import a single type from a module to get around this.

import class APIService.Item

Then you can refer to Item directly without confusion.

The class, protocol, struct, enum and func keywords (at least) work in this situation.

like image 137
Rob Wettach Avatar answered Nov 15 '22 16:11

Rob Wettach