3

I'm developing a framework as a component to a project. The framework has some networking components and basically I want the path to change depending on the release configuration of the main app.

It's set-up as such:

let path = //development path

And id like to create a flag such that:

#if PRODUCTION
let path = //production path
#else 
let path = //dev path 
#endif

These release schemes are specific to the main app which has loaded the framework in. Is it still possible to use a compiler flag to define this behavior?

wadda_wadda
  • 966
  • 1
  • 8
  • 29
  • Possible duplicate of [#ifdef replacement in swift language](http://stackoverflow.com/questions/24003291/ifdef-replacement-in-swift-language) – Kurt Revis May 04 '16 at 05:17
  • 1
    Although that won't work if you are building the framework and the app as separate projects. Since you are building the framework first, there is no way any compiler flags or configuration for the app can directly affect the compiled code in the framework. However, your app _can_ pass different parameters to the framework. – Kurt Revis May 04 '16 at 05:22
  • @KurtRevis I am indeed building them as separate projects. Can you explain how I would pass parameters to the framework? Are you implying having a public network manager class within the framework that gets passed a base path dependent on the scheme of the main app? – wadda_wadda May 04 '16 at 05:25
  • 1
    Exactly. Your app (which knows whether it's PRODUCTION or not) needs to tell the framework (which doesn't) what to do. Your app presumably calls some code in the framework -- how you structure it is up to you. It could be a whole path, or just a boolean for production/non-production, or anything you like. – Kurt Revis May 04 '16 at 05:40
  • You could argue that the framework should always obtain the path from the main app by (for example) a function call. That way, you get maximum flexibiltiy For example you could set a different path for unit tests etc. – JeremyP May 04 '17 at 13:57

2 Answers2

1

you could create a global

var production: Bool!

in framework that should be set in an app using the framework

Anton Tropashko
  • 5,486
  • 5
  • 41
  • 66
0

This has been long figured out, but basically it was as simple as passing the dependencies in during a configure function.

let dependencies = MyDependencies(...)
MyFramework.configure(with: dependencies)
wadda_wadda
  • 966
  • 1
  • 8
  • 29