Mobile Development
Swift
Subjective
Oct 04, 2025
Explain Swift's module system and access control.
Detailed Explanation
Swift's module system provides encapsulation and access control at different levels.\n\n• **Module Structure:**\n\n// Module: MyLibrary\n// File: PublicAPI.swift\npublic struct PublicType {
\n let publicProperty: String\n let internalProperty: Int\n let privateProperty: Bool\n \n public init(publicProperty: String) {\n self.publicProperty = publicProperty\n self.internalProperty = 0\n self.privateProperty = false\n }\n}\n\n\n• **Access Levels:**\n• **open:** Subclassable outside module\n• **public:** Accessible outside module\n• **internal:** Accessible within module (default)\n• **fileprivate:** Accessible within file\n• **private:** Accessible within declaration\n\n\nopen class OpenClass {
\n var publicVar = 0\n var internalVar = 0\n var filePrivateVar = 0\n var privateVar = 0\n \n func openMethod() {}\n func publicMethod() {}\n func internalMethod() {}\n func filePrivateMethod() {}\n func privateMethod() {}\n}\n\n\n• **Module Imports:**\n\n// Import entire module\nimport Foundation\n\n// Import specific declarations\nimport struct Foundation.URL\ func Darwin.sqrt\n\n// Import with custom name\nimport MyVeryLongModuleName as Short\n\n\n• **Conditional Compilation:**\n\n#if canImport(UIKit)\nimport UIKit\ntypealias PlatformView = UIView\n#elseif canImport(AppKit)\nimport AppKit\ntypealias PlatformView = NSView\n#endif\n
Discussion (0)
No comments yet. Be the first to share your thoughts!
Share Your Thoughts