Access Permission

I’ve been enjoying the book, but the way access permissions for properties etc is used in examples seems odd to me. By default, “internal” level access is enough to access a property in the same module, so why is “public” used so often in the book examples? Also, since Swift 4, “private” allows access to the same class in the same file and appears to be preferable to “fileprivate” in most contexts, but is there a specific reason that “fileprivate” is used so often in the book?

@jstrawn Can you please help with this when you get a chance? Thank you - much appreciated! :]

I’m glad to hear you’re enjoying the book, @yhkaplan ! :]

Your post has two questions, so I’ll answer each in turn:

It’s true that internal provides access to all other classes throughout the module, which includes an “app” module.

However, will these classes always be directly in the app? Who’s to say you later won’t want to break them up into separate modules?

There’s no harm in marking methods and properties as public even when they are used only in a single module, including an app module.

However, if you don’t mark methods and properties as public that should be accessible to other modules, you’re going to have a really hard time should you later decide to pull some classes into separate modules.

Hence, throughout the book, we chose to mark methods and properties as public when their use case would justify “this should be accessible even if this class was in a different module.”

You’re right! private should indeed be preferred to fileprivate. Since private properties/methods/etc are now available even within extensions of classes, the use case for fileprivate is very narrow (honestly, I’m struggling to think of a good one right now even!)

We started writing this book right before Swift 4 was released. Because Swift 3.X did not expose private properties/methods/etc to extensions, I suspect they were marked as fileprivate instead for this reason.

In updating the book to Swift 4.X, however, it seems some references were missed… :scream_cat:

However, we’re in the process of updating the book right now for the latest-and-great version of Swift and iOS! And if you purchased the digital book, the good news is you’ll get this update for free! :]

I’ll make sure we change fileprivate to private throughout appropriately. Thanks for bringing this to our attention!