What is the difference between private and fileprivate in Swift 4
Asked Answered
U

7

12

In Swift 4, since now private is visible in extensions also in the same source code file, how is it different from the fileprivate access modifier?

Background: In Swift 3, private variables in a class are not visible in its extensions in the same file. For that, fileprivate had to be used.

Ungodly answered 5/10, 2017 at 14:53 Comment(0)
G
38

File Private
File-private access restricts the use of an entity to its own defining source file. Use file-private access to hide the implementation details of a specific piece of functionality when those details are used within an entire file.
Syntax: fileprivate <var type> <variable name>
Example: fileprivate class SomeFilePrivateClass {}


Private
Private access restricts the use of an entity to the enclosing declaration, and to extensions of that declaration that are in the same file. Use private access to hide the implementation details of a specific piece of functionality when those details are used only within a single declaration.
Syntax: private <var type> <variable name>
Example: private class SomePrivateClass {}


Here is more detail about all access levels: Swift - Access Levels

Answer to your question: (In Swift 3, private variables in a class are not visible in its extensions in the same file. For that, fileprivate had to be used.)

Yes, in Swift 4.0, Private is now accessible in extension but within same file. If you declare/define extension in other file, then your private variable will not be accessible to your extension


Look at this images:
File: ViewController.swift
Here extension and view controller both are in same file, hence private variable testPrivateAccessLevel is accessible in extension

enter image description here


File: TestFile.swift
Here extension and view controller both are in different files, hence private variable testPrivateAccessLevel is not accessible in extension.

enter image description here

enter image description here


Here class ViewController2 is a subclass of ViewController and both are in same file. Here private variable testPrivateAccessLevel is not accessible in Subclass but fileprivate is accessible in subclass.

enter image description here

Gadson answered 5/10, 2017 at 15:1 Comment(5)
It looks like private and file private can be interchangably be used now. Can you provide an example where private can be used but not fileprivate or vice versa.Ungodly
Thanks for the example. I understood your point. But my concern is if in the above example, I swap the keywords private and fileprivate, the output will not change.Ungodly
Nope, if you move your extension in another file fileprivate won't be accessible too.Ungodly
I guess in this case both the access modifiers are similar. They must be different in other cases like in sub classes. You cannot access a private variable from sub class even within the same file.Ungodly
That's helpful!Jeth
S
9

Applicable in swift 4.0 and its versions
Private
Private access only in class and its extension(When extension is in the same .swift file).

File Private
File-private access only in class and its extension & subClass(When extension or subClass is in the same .swift file).

Stereochromy answered 18/9, 2018 at 11:32 Comment(0)
P
0
///////////////ViewController1.swift file
        class ViewController1 {
            private func testPrivate() {
                print("testPrivate")
            }
            fileprivate func testFilePrivate() {
                print("testFilePrivate")
            }
            func doesNothing1() {
                testPrivate() //success
                testFilePrivate() //success
            }
        }
        extension ViewController1 {
            func doesNothingInExtensionSameFile() {
                testPrivate() //success
                testFilePrivate() //success
            }
        }
        class SomeOtherClassInSameFile {
            let vc1 = ViewController1()
            func doesNothing() {
               vc1.testPrivate() //throws error
               vc1.testFilePrivate() //success
            } 
        }
////////////// ViewController2.swift file
        extension ViewController1 {
            func doesNothingInExtensionDifferentFile() {
                testPrivate() //throws error
                testFilePrivate() //throws error
            }
        }
Photoelasticity answered 12/2, 2019 at 15:11 Comment(0)
D
0

private and fileprivate access levels have come closer with Swift4.

The difference in access lies as follows:

fileprivate members - only & entirely within that .swift file

private members - only in that class & extension of the class if both are present in same .swift file

Hence only fileprivate members(not private) can be accessed in

  • Sub Classes in the same .swift file
  • Instances of the class (initialized in another class) in the same .swift file.
Disquisition answered 29/7, 2019 at 12:21 Comment(0)
C
0

Open Vs Public:

  • Public does not allow a class to be inherited in another module/target whereas Open does.
  • Public method does not allow to be overridden in subclass in another module/target whereas Open does.

Apart from above both are same.

Private Vs Fileprivate:

  • (Within single file) Private does not allow to access (func and properties) in subclass whereas FilePrivate does.
  • (Outside File) Private and FilePrivate both can't be accessible.

Apart from above both are same.

Charentemaritime answered 19/10, 2020 at 4:55 Comment(1)
@Cristik yes, file private is for entire file, but I was comparing private and file private. Private does not allow to access inside subclass. While comparing, we have to say like that way. In the file we can have class, extensions, and subclass, so private can allow to access in class itself and extension but not subclass within the specific file.Charentemaritime
V
-2

"Private" is accessible only in class, "FilePrivate" accessible only in .swift file.

Verboten answered 18/9, 2018 at 7:16 Comment(1)
(This post does not seem to provide a quality answer to the question. Please either edit your answer, or just post it as a comment to the question.)Lieu
S
-3

Private : Access in Class and Class Extension. FilePrivate : Access in class, subClass, Extension,

Sterner answered 28/7, 2019 at 12:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.