43

I'm confused about the internal and private access modifier.

The docs says:

“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.”

How I thought it was, was that with internal you can access everything if you are in your own app. But this is not true, because when I have a viewcontroller what is default internal and I'm having a internal function on that viewcontroller I can't access this from another file in another group (You create these in xCode).

What I tried was having a ViewController that has a method foo in group A then in group B I created a ViewController like this:

let vc: FakeViewController = FakeViewController()
vc.foo()

So is internal restricted to the same group? Or I'm I interpreting it wrong?

Is it useful that in a viewcontroller you create private methods and vars/lets?

user1007522
  • 7,858
  • 17
  • 69
  • 113
  • 3
    What do you mean by "group"? – zaph Feb 09 '16 at 14:20
  • The groups you can create in xCode. I don't know if it has something to do with. – user1007522 Feb 09 '16 at 14:29
  • 1
    The groups have no effect on access control, they're merely a tool for organising a project/workspace. The documentation is not wrong, so if you can't access an `internal func` from somewhere it must be because it's in a different module somehow. – Mike Pollard Feb 09 '16 at 14:36
  • Refer the below link you will get proper Description with an example. https://developer.apple.com/swift/blog/?id=5 – Gautam Sareriya Oct 06 '16 at 05:37

5 Answers5

47

@user1007522 Could you post the entire source code for FakeViewController? You should have access to foo() from your vc variable. If you do not, I suspect something else is in play here.

I found the following definitions much easier to understand (copied from UseYourLoaf - Swift 4 Access Levels)

The Five Access Levels of Swift 3/4

Swift 3 has five access levels that control from which source file or module you can access something. In order from most open to most restricted:

  • open you can access open classes and class members from any source file in the defining module or any module that imports that module. You can subclass an open class or override an open class member both within their defining module and any module that imports that module.

  • public allows the same access as open - any source file in any module - but has more restrictive subclassing and overriding. You can only subclass a public class within the same module. A public class member can only be overriden by subclasses in the same module. This is important if you are writing a framework. If you want a user of that framework to be able to subclass a class or override a method you must make it open.

  • internal allows use from any source file in the defining module but not from outside that module. This is generally the default access level.

  • fileprivate allows use only within the defining source file.

  • private Swift 4: allows use only from the enclosing declaration and new in Swift 4, to any extensions of that declaration in the same source file Swift 3: allows use only from the enclosing declaration.

Peter
  • 29
  • 1
  • 5
Michael Peterson
  • 10,383
  • 3
  • 54
  • 51
13

Suppose you have 3 different view controller source files A, B, C then In Private:- If Intancses in A are Private than only A's Methods can use them In Internal :- IF A is as Internal than B and C can easily use them. Here is an example: enter image description here

Thanks

nacho4d
  • 43,720
  • 45
  • 157
  • 240
singh.jitendra
  • 490
  • 1
  • 9
  • 19
9

Internal access restricts access to the files within a singular application or framework.

Private restricts access to the individual source file that your object is created in.

See this link for a more in-depth explanation.

Overall, if your "Group A" and "Group B" are in the same application or framework, you should be able to access the methods from each, assuming the viewController allows internal access.

Benjamin Lowry
  • 3,730
  • 1
  • 23
  • 27
  • 5
    Not quite right, from [Apples documentation](https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/AccessControl.html) 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**. A module != singular application or framework. The language in the cited link is a little sloppy. – zaph Feb 09 '16 at 14:49
  • My Group A and B are in the same module I guess. It's in the same app. Thats what I find confusing the module part. – user1007522 Feb 09 '16 at 18:32
  • The comment by @Mike Pollard is correct, groups are nothing but organization in Xcode, they have no effect on the code or app. – zaph Feb 09 '16 at 18:38
  • @zaph: An app and a framework target are examples of **module** according to [this article](https://developer.apple.com/swift/blog/?id=5) . – Tim Sep 22 '16 at 01:19
0

A little more visual explanation:

1

stackich
  • 3,607
  • 3
  • 17
  • 41
-1

My understanding is that private won't allow the variable from being accessed from outside that class. However, there are times, like with gesture recognizers, you can't make them private because they are needed behind the scenes. Marking them as "internal" lets them be accessed from within other functions, but not called directly.

Mostly I use internal to keep my code organized, so I know that's not a public facing function but it can still be used.

EdwardSanchez
  • 95
  • 1
  • 8
  • 3
    Not quite right. `private` has nothing to do with classes. It restricts access to the same source file (Swift 2) or to the enclosing scope (Swift 3). Note that when you declare something in a class definition, the enclosing scope does not extend to other `extension`s. That way, private functions are not available in the whole class. – Tali Mar 09 '17 at 06:53