Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why private is unaccessible to the extension?

Here is my ViewController.swift file:

class ViewController: UIViewController {
    private var myInt = 10
}

extension ViewController {
    func printMyInt() {
        print(myInt)
    }
}

Although as mentioned in the Swift documentation - Access Levels section:

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.

Since Swift 4 has been released, I assume that I am able to implement such a thing (it is also mentioned in: What's New in Swift - WWDC 2017 session), however, the complier shows me:

enter image description here

'myInt' is inaccessible due to 'private' protection level

Is it incompatible with what mentioned in the documentation?


As a simple quick solution, I could declare it as:

fileprivate var myInt = 10

but I wonder why is it behaves like this, am I misunderstand what mentioned in the documentation? or is it a "Xcode" bug (used 9.0.1 version)?

Remark: The project has been created in the older Xcode 8 and then migrated to Xcode 9.

like image 609
Ahmad F Avatar asked Oct 22 '17 07:10

Ahmad F


People also ask

Can we access private in extension Swift?

In Swift 4, an extension can reach across to a private declaration. This only applies to extensions that live in the same source file as the private declaration. In other words, an entity that is declared private is accessible from within any extensions for that type within the same source file.

Can we use Fileprivate in extension?

fileprivate allows use only within the defining source file. private allows use only from the enclosing declaration and since Swift 4, to any extensions of that declaration in the same source file.

What is fileprivate?

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 internal in Swift?

Internal access enables entities to be used within any source file from their defining module, but not in any source file outside of that module. You typically use internal access when defining an app's or a framework's internal structure.


1 Answers

In Swift 4, private members are accessible to extensions of that declaration that are in the same file, see SE-0169 – Improve Interaction Between private Declarations and Extensions.

If the project has been created in Xcode 8 (with Swift 3) then Xcode 9 will open it in "Swift 3 mode" and set the "Swift Language Version" to "Swift 3.2". Therefore the stricter Swift 3 restrictions hold.

To make the private extension visible to extension in the same file, set the Swift language version to 4 in the build settings. (Of course that might make more changes in your code necessary.)

Even better, use "Edit -> Convert -> To Current Swift Syntax ..."

like image 77
Martin R Avatar answered Sep 19 '22 12:09

Martin R