Swift conditional compilation access control
Asked Answered
N

1

7

I am building an iOS framework and i need the same framework to be used in public apps and in some internal apps , the difference is that in the internal apps I need access to some classes and function that I want to hide from the public apps and i don't want to copy paste code from the framework to the internal apps ...

Is it possible to use conditional compilation (or some other solution without duplicating code) to do something like :

#if SOMEFLAG
   public
#else
   internal
#endif
   class SomeClass{
       // public when SOMEFLAG otherwise internal
   }

of course the code above is not working so it is the pseudo code for what i need ( it doesn't have to be with macros) .

Thanks.

Namtar answered 10/11, 2016 at 7:33 Comment(7)
Possible duplicate of #ifdef replacement in swift languageDonner
@Donner the link doesn't work for access control, my question is about conditional access control without duplicating code , it doesn't have to be with macros.Namtar
It is not possible to define in that way, Swift Compiler will through the error. How are you planning to distribute the framework.Cottrell
I will give the clients the .framework file directly, i don't want to give them the source code.Namtar
@OlegSherman did you get any solution?Sacramentalist
@Sacramentalist I did not get a solution yet.Namtar
@OlegSherman i am starting to read this developer.apple.com/library/content/documentation/… will let you know if i get a solution.If you get it earlier do let me knowSacramentalist
E
0

Not exactly what you want since you want to define access on the class but you can do the following on a method level:

#if SOMEFLAG
    public func doSomething() {
        internalDoSomething()
    }
#endif

internal func internalDoSomething() {
    // ...
}

and use internalReload() instead of doSomething() in your internal code.

Ephemera answered 25/4 at 15:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.