Home > Blockchain >  How to avoid having large files in Swift?
How to avoid having large files in Swift?

Time:02-17

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?

CodePudding user response:

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.

CodePudding user response:

This doesn't come up in iOS programming much, but I think a lot of iOS programmers know this instinctively, make you model classes just store values, and maybe some really basic methods on them, then you have model controllers that can modify the model classes, in Mac OS we always talked about model, model controllers, view controllers, and views, but in reality I think this was the model, view, control pattern and the model, view, presenter pattern combined, and model controls where the real controllers, and view controllers where the presents.

  • Related