2

I have a set of classes in a subfolder (along with a corresponding group in Xcode) that implement the facade pattern: only one class should be used from outside this folder. How do I implement this access control in Swift?

In Java, I'd declare the facade class public, and leave everything else with package access.

In Swift, it seems I should use internal access, defining another module for the folder. And in Xcode, modules correspond to build targets. So, how do I define a build target for a subfolder, in the Xcode GUI? Note that it should all be linked together into one binary.

Will defining another build target add more maintenance overhead in the future? I'm not familiar with the Xcode build system.

Kartick Vaddadi
  • 4,818
  • 6
  • 39
  • 55
  • You should convert those folder on a Swift independent target inside your project. All your classes except that which implements the façade pattern must be internal. The façade one must be public. Take a look at this Technical Note from Apple https://developer.apple.com/library/prerelease/content/technotes/tn2435/_index.html#//apple_ref/doc/uid/DTS40017543 – Adolfo Feb 09 '17 at 14:37
  • I prefer to link statically, to keep things simple, rather than have a framework. How do I do that, while sharing the main app's build settings? @Adolfo – Kartick Vaddadi Feb 10 '17 at 03:58
  • The isolation leven that you want to obtain it's not possible in Swift if classes are inside the same target. In Swift documentation you will find description about access control and their effects inside project/module https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/AccessControl.html – Adolfo Feb 10 '17 at 07:39
  • I know that. I was wondering if I could create a target that creates a static library, rather than a framework. – Kartick Vaddadi Feb 10 '17 at 09:15

0 Answers0