Reputation: 449
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.
Upvotes: 12
Views: 9398
Reputation: 1006
Open Vs Public:
Apart from above both are same.
Private Vs Fileprivate:
Apart from above both are same.
Upvotes: 0
Reputation: 51
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
Upvotes: 0
Reputation: 1
Private : Access in Class and Class Extension. FilePrivate : Access in class, subClass, Extension,
Upvotes: -3
Reputation: 17
///////////////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
}
}
Upvotes: 0
Reputation: 1123
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).
Upvotes: 9
Reputation: 47
"Private" is accessible only in class, "FilePrivate" accessible only in .swift file.
Upvotes: -2
Reputation: 79706
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
File: TestFile.swift
Here extension and view controller both are in different files, hence private variable testPrivateAccessLevel
is not accessible in extension.
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.
Upvotes: 38