Vijayakrishna Kalluraya Vijayakrishna Kalluraya - 7 months ago 34
Swift Question

Swift Control access per targets

Does any one know how to have different swift control access for different targets. Basically I have an iOS framework in swift with two targets A and B. I wanted a class say "Hello" as public in target A and internal in target B. One of the way to have this define a Swift flag and have something like this.

#if FLAG

public class Hello {

#else
class Hello {

#endif


Open brace with no closing brace in the same scope does compile in swift. One of the possible way to have the empty class under flag and put the rest in an extension. This is not a good solution since I need to make some of the functions also under flag to control the access. Is there any solution to control the access without duplicating the functions ?

Or is the approach fundamentally wrong ? I need to have a wrapper for the class to make it public ?

Answer

Unfortunately, this isn't something you can do in Swift. In Objective-C you could do tricks like this and the compiler would ignore anything that wasn't valid inside the macros that weren't excluded. This is not the case for Swift. The entire file must be valid, including parts that are being ignored because of the #if's