can we declare public property in internal class or struct

Generated on 8/7/2024

1 search

Yes, you can declare a public property in an internal class or struct in Swift. The access control levels in Swift allow for this kind of declaration.

From the session A Swift Tour: Explore Swift’s features and design, it is mentioned that:

"Public is one of several different access control levels available in Swift. There are also private, internal, and package levels. A declaration that is marked private can only be accessed by code in the same file. An internal declaration can only be accessed by other code in the same module. Whenever you don't specify an access level, Swift implicitly uses internal package declarations are accessible from other modules in the same package, and public declarations are accessible from any other module."

This means that you can have an internal class or struct with public properties, making those properties accessible from any other module, while the class or struct itself remains accessible only within the same module.