Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between private and fileprivate in Swift 4

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.

like image 205
crypt Avatar asked Oct 05 '17 14:10

crypt


People also ask

What is the difference between Fileprivate and private?

fileprivate means an entity that is accessible anywhere in that file. private means an entity that cannot be accessed anywhere except for the enclosing type, such as a class.

What is Fileprivate in Swift?

Fileprivate access restricts the use of an entity within the same defined source file. The only reason you would use fileprivate is when you want to access your code within the same file from different classes or structs.

What is the difference between open and public and Fileprivate and private?

Open - same as public, only difference is you can subclass or override outside the module. Fileprivate - As the name say's, data members and member functions are accessible within the same file. Private - This is where you can have access within the scope of function body or class.

Can we use Fileprivate in extension?

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. fileprivate is accessible within same file.

What is the difference between fileprivate and private access in Swift?

Private access only in class and its extension (When extension is in the same .swift file). File-private access only in class and its extension & subClass (When extension or subClass is in the same .swift file). private and fileprivate access levels have come closer with Swift4.

What is the difference between file private and file private?

(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.

How to view private variables in Swift 3 class extensions?

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.)

What happened to the fileprivate keyword in Swift?

The fileprivate keyword took over the role of the private keyword and the latter became more restrictive. Even though many developers asked for the removal of the fileprivate keyword, the Swift team came up with an alternative that only modified the meaning of the private keyword.


2 Answers

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

like image 183
Krunal Avatar answered Nov 08 '22 17:11

Krunal


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).

like image 37
Vaibhav Sharma Avatar answered Nov 08 '22 18:11

Vaibhav Sharma