Search code examples
iosswiftencapsulation

Is encapsulation in Swift as important as in other OO languages


When I was learning other languages such as Java, all the sample code tells you that you should always mark a field private. And the methods that do the internal stuff are also marked private or protected.

But when it comes to Swift, I see that the sample code from a lot of the tutorials (such as raywenderlich.com) seldom marks something as private. They just leave it internal (without adding any modifiers).

In contrast, all the android tutorials mark some of the fields as private and provide no getters or setters for them.

For example, I read this search bar tutorial a while ago:

https://www.raywenderlich.com/113772/uisearchcontroller-tutorial

When I was reading through it, I was thinking about: "Why did you not mark this private?"

let searchController = UISearchController(searchResultsController: nil)

I mean, it does not make sense for other classes to access the search controller of my VC. It's mine!

Question:

Why don't most, if not all tutorials provide sample code that contains private properties? Is it because iOS is more secure or something? Should I mark suitable stuff as private in production code?

I hope this is not too opinion based. In Java, you will never make a field public, right? That's just a fact - you should not make fields public. So please don't close as opinion based, it's not.


Solution

  • Why not everything private? There are several really common scenarios that I've seen that always favor having the variable either internal or public.

    Delegate methods:

    func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell     {
        let cell = tableView.dequeueReusableCellWithIdentifier(
            myCellID,
            forIndexPath: indexPath);
        return cell;
    }
    

    This method will be called not internally by you, but rather by the iOS system that manages and displays the tableView. The delegation pattern is widely used within iOS development.

    Data exchange between two ViewControllers, where you simply want to set the other ViewController's field:

    class MyVC1: UIViewController {
    
       var flagShouldUpateColors = false {
          didSet{ 
                // some stuff
          }
       }
    
    
    }
    

    Then at some point you want to notifiy this ViewController that the flag should be set to true. You do that my simply acccessing it's field.

    Also selectors. When I register an instance of a ViewController for some notification, I pass the name of a method that I have within the very same ViewController. It is to be called when the notification triggers. Basically what the code says is "please call this method for me when this occurs". If you say call this private method when this occurs, it wouldn't make sense would it ? How is the caller supposed to call it(aside from reflection and other unoptimized ways). Here is example:

    override func viewDidLoad() {
        NSNotificationCenter.defaultCenter().addObserver(
            self,
            selector: #selector(self.doStuffWhenEnterForeground),
            name: UIApplicationWillEnterForegroundNotification,
            object: app)
        doStuffWhenEnterForeground();
    }
    func doStuffWhenEnterForeground() {
    
    }