https://stackoverflow.com/questions/41741955/in-swift-what-is-the-difference-between-the-access-modifiers-internal-and-publi
open means classes and class members can be subclassed and overridden both within and outside the defining module (target).
In other words, an open class is accessible and subclassable outside of the defining module. An open class member is accessible and overridable outside of the defining module.
fileprivate restricts the use of an entity to its defining source file. Basically accessible by multiple classes within a single file.
private restricts the use of an entity to its enclosing declaration.
internal enables an entity to be used within the defining module (target). Also, this happens to be the default specifier if nothing else is mentioned. We would typically use internal access when defining an app’s or a framework’s internal structure.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
class Test { // Open access and public access enable entities // to be used within any source // file from their defining module, // and also in a source file from another module // that imports the defining module. You typically use open or // public access when specifying the public interface to a framework. public var date: String public var score: Int // 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. internal var curveScore: Double // 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. fileprivate var passed: Bool // Private access restricts the use of an entity to the enclosing declaration. // Use private access to hide the implementation details of a specific piece // of functionality when those details are used only within a single declaration. private var studentSSN: String init() { self.date = "NA"; self.score = 0; self.curveScore = 0.0; self.passed = false; self.studentSSN = ""; } } struct Midterm { private var exam: Test? init() { exam = Test() //if the exam is not nil if let myExam = exam { myExam.date = "haha" myExam.score = 24 myExam.curveScore = 24.3 myExam.passed = true; // noticed how we can use passed here. // because we are in the same file, we can access property passed in MyExam. //However, in main.swift, you can't } } } |