Linked Questions
17 questions linked to/from Does Swift have access modifiers?
22 votes
3 answers
20k views
How do I make a private function in Swift? [duplicate]
How do I make a private function in Swift? Here is an example: import UIKit class AnotherClass { var someVar = 1 let someConst = 2 func somePrivateFunc() -> Bool { return ...
11 votes
1 answer
8k views
How are private properties used in the Swift language? [duplicate]
It seems that there is not a private, or for that matter public, keyword in swift language. So it is possible at all to have a private property? On a side note, Swift is very much similar to ...
4 votes
0 answers
266 views
Swift Members / Methods Accessibility Modifiers [duplicate]
I have a very simple Swift question for which there is probably an obvious answer, but I can't seem to find it. How are methods and variables within a class declared as: Private? Protected (i.e. ...
398 votes
5 answers
152k views
What is the 'open' keyword in Swift?
The ObjectiveC.swift file from the standard library contains the following few lines of code around line 228: extension NSObject : Equatable, Hashable { /// ... open var hashValue: Int { ...
113 votes
11 answers
36k views
Computed read-only property vs function in Swift
In the Introduction to Swift WWDC session, a read-only property description is demonstrated: class Vehicle { var numberOfWheels = 0 var description: String { return "\(...
48 votes
5 answers
39k views
How to declare a 'protected' variable in swift
I want to create a class that inherits from another class, which is in a different file. For example: Class1.swift class Class1 { protected var //Do Stuff } Class2.swift class Class2:Class1 {...
30 votes
4 answers
34k views
Static Library and Swift
So I'm working on an iOS project in Swift, and I wanted to create a Static library with some useful stuff in it. My problem is when I try to build my lib in Xcode (version 6.3) I have a "Build Failed"...
19 votes
6 answers
21k views
Swift 3: The difference between Public and Internal access modifiers? [duplicate]
I read the Apple's reference about access modifiers in Swift 3. I read also about the same on stackoverflow but I didn't get an answer as the person who asked. As I understood correctly, there are ...
6 votes
2 answers
3k views
How do I restrict a property to a range?
There are a cases where I might want to model data where it makes sense for a value to be restricted to a given range. For example, if I want to represent a "mammal", I might want to restrict a legs ...
6 votes
3 answers
5k views
Data encapsulation in Swift
I've read the entire Swift book, and watched all the WWDC videos (all of which I heartily recommend). One thing I'm worried about is data encapsulation. Consider the following (entirely contrived) ...
8 votes
2 answers
4k views
In Swift, what is the difference between the Access modifiers internal and public?
Swift offers 5 access modifiers: open, public, internal, fileprivate and private. Of what I know about these specifiers, (mainly from link & link_2) open means classes and class members can be ...
9 votes
1 answer
3k views
Swift setter and getter issues [closed]
I know there are a few questions regarding with this already. And I know swift can only customise property setter and getter for computed properties. But I think this is the worst part of Swift. ...
3 votes
1 answer
980 views
Referring to a type in another module with the same name as a type in the current module when the other module has a type with its own name in Swift
I am using the swift package manager. I have a module ModuleA which exports two types: ModuleA and Test. I have a module ModuleB which defines a single type: Test. In ModuleB, how can I refer to the ...
2 votes
2 answers
387 views
Information Hiding the "Swifter" way?
I have a question regarding object oriented design principles and Swift. I am pretty familiar with Java and I am currently taking a udacity course to get a first hands on in Swift. In the Java ...
0 votes
1 answer
869 views
Public Vs Internal Access Modifier
I have gone through the Apple provided document i.e https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/AccessControl.html Here, it explains: ...