Swift 2 internal vs private
Asked Answered
B

5

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?

Braunstein answered 9/2, 2016 at 14:5 Comment(4)
What do you mean by "group"?Karren
The groups you can create in xCode. I don't know if it has something to do with.Braunstein
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.Papandreou
Refer the below link you will get proper Description with an example. developer.apple.com/swift/blog/?id=5Backstretch
C
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.

Contemporary answered 8/1, 2018 at 12:43 Comment(0)
I
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

Induna answered 14/4, 2017 at 5:38 Comment(0)
P
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.

Pilar answered 9/2, 2016 at 14:30 Comment(4)
Not quite right, from Apples documentation 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.Karren
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.Braunstein
The comment by @Mike Pollard is correct, groups are nothing but organization in Xcode, they have no effect on the code or app.Karren
@zaph: An app and a framework target are examples of module according to this article .Riki
R
1

A little more visual explanation:

1

Rattlesnake answered 24/8, 2023 at 0:39 Comment(0)
D
-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.

Determinative answered 9/3, 2017 at 5:54 Comment(1)
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 extensions. That way, private functions are not available in the whole class.Verdict

© 2022 - 2024 — McMap. All rights reserved.