Swift Access Controls: How To Avoid The ‘Private’ Keyword Repetition

A few weeks ago, I contributed an article about Swift functions in objc.io, where I mentioned that I prefer to put all my private methods together at the bottom of my class, separated by a landmark. So I wish there was a way in Swift, like in other languages, to say that all functions below this one are private.

In response, @chromophoreapp had the following idea:

I tried this out today, and really like the result!

import UIKit

class ViewController: UIViewController {

    override func viewDidLoad() {


// MARK: Private
private extension ViewController {
    func configureAppearance() {
        view.backgroundColor = UIColor.purpleColor()

This approach really reduces the noise in your ViewController, and has the added benefit of not having to write the private keyword over and over again for your private methods.

Enjoy the article? Join over 20,000+ Swift developers and enthusiasts who get my weekly updates.

  • Jason R Tibbetts

    That’s cool to know, but it seems like more trouble than it’s worth. I used to put private methods at the bottom of the class file, too, but now I find it more useful to group them with whatever public methods use them.

    • I’ve been using this way for the past week, and really like it. It really brings out the main functionality of the class at first glance. I do wish I could name extensions to group some of the private methods together more.

  • robertjpayne

    Using this methodology also prevents you from creating private stored properties.

    • Neeraj Kumar

      but in lot of cases of i would want to create stored properties in that case I have to define them at the top with private.

  • Peter Witham

    There maybe downsides that I’ve not come across, but I really like this idea since I’m OCD about how my source code looks πŸ™‚

  • Antonio Bello

    It was so simple that I didn’t think about it… I think I’ll be busy in the next hour applying that change πŸ™‚ Thanks for sharing that, very helpful. Any brilliant workaround like that, but for the protected keyword, which I miss a lot? πŸ™‚

  • Neeraj Kumar

    @Natasha but most of the times there are stored properties in class which can’t be defined in extension.

    • Yes, you’d have to place your private properties at the top of the class. Still really loving the private extensions for functions though πŸ™‚