Swift private access level change in Swift 4

⋅ 3 min read ⋅ Swift

Table of Contents

Swift keeps improving and changing every year. So, it is not a surprise if you miss one or two changes in the language.

This article will discuss change in one of the Swift access levels, private, which has happened since Swift 4.

I will talk about the behavior of private before and after the changes.

Before Swift 4

private access level before Swift 4 is straightforward. It restricts the use of an entity to the enclosing declaration.

We use this access level for any properties or methods that we want to use internally within a class or struct.

In the following example, I declare an onlyUnreadMessage variable private because I mean to use it only within the class.

class MessageViewController: UIViewController {
private var onlyUnreadMessage: Bool = false
}

As time passes, I make this view controller adopt the UITableViewDataSource protocol and want to access the onlyUnreadMessage, but I won't be able to access it due to the private access level.

extension MessageViewController: UITableViewDataSource {

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// Error - inaccessable (Swift 3)
if onlyUnreadMessage {
return 10
} else {
return 20
}
}
}

Before Swift 4, the private access level isn't visible from within an extension.

fileprivate to the rescue

Since adopting protocol via extension is so widely adopted in the Swift community. You can see people over this limitation by using fileprivate access level instead.

class MessageViewController: UIViewController {
fileprivate var onlyUnreadMessage: Bool = false
}

By changing private to fileprivate, you can access onlyUnreadMessage from an extension in the same source file.

The following code would no longer produce an error.

// MessageViewController.swift
class MessageViewController: UIViewController {
fileprivate var onlyUnreadMessage: Bool = false
}

extension MessageViewController: UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
if onlyUnreadMessage {
return 10
} else {
return 20
}
}
}

You can easily support sarunw.com by checking out this sponsor.

Sponsor sarunw.com and reach thousands of iOS developers.

private access level changes in Swift 4

The fact that Swift encourages developers to use extensions as a logical grouping mechanism and requires them for conditional conformances make fileprivate more need than expected.

This widely adopt of the said pattern confuse the use of those two levels (private and fileprivate).

In Swift 4, Swift extends the scope of private to cover that specific scenario (SE-0169).

So, in Swift 4, the private members of a type can be accessed from an extension of that type in the same file.

With this change, the following code is valid.

class MessageViewController: UIViewController {
// 1
private var onlyUnreadMessage: Bool = false
}

extension MessageViewController: UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
// 2
if onlyUnreadMessage {
return 10
} else {
return 20
}
}
}

1 We declare onlyUnreadMessage with a private access level.
2 And we can access it in the extension in the same file without the need for fileprivate.

You can easily support sarunw.com by checking out this sponsor.

Sponsor sarunw.com and reach thousands of iOS developers.

Conclusion

With this change, you probably see less and less use of the fileprivate access level.

So, if you are an experienced developer, don't surprise if a newcomer doesn't know about fileprivate anymore and questions why you use them.

If you are new to Swift development, this change is probably the reason why you see fileprivate in legacy code.


Read more article about Swift or see all available topic

Enjoy the read?

If you enjoy this article, you can subscribe to the weekly newsletter.
Every Friday, you'll get a quick recap of all articles and tips posted on this site. No strings attached. Unsubscribe anytime.

Feel free to follow me on Twitter and ask your questions related to this post. Thanks for reading and see you next time.

If you enjoy my writing, please check out my Patreon https://www.patreon.com/sarunw and become my supporter. Sharing the article is also greatly appreciated.

Become a patron Buy me a coffee Tweet Share
Previous
Easy way to Format Date in SwiftUI Text

Since iOS 14, SwiftUI Text has had many initializers dedicated to presenting dates. Let's explore all of them.

Next
What is SwiftUI Form

Learn what Form is, how to use it, and the difference from a List view.

← Home