swiftaccess-levels

How to avoid having large files in Swift?


So I have a Swift file with some class Whatever. This class has a number of private properties. Like this:

class Whatever
{
    private let privateString = "Blabla"
    private let privateInt    = 125
    
    // a lot of code here
 }

I would like to create an extension for this class in a separate file. Just in order to avoid having a large file with enormous amount of code. But I can't. An extension in a separate file cannot access private properties of the class. So I'm forced to either make private properties internal or maintain a single large file. Is there any technical solution to this problem except creating a module for this functionality?


Solution

  • You can't access private properties from another files. The only thing I can think of that may help you is to use to replace private with private(set) which provide you a read-only access from other files.