Skip to main content
edited tags
Link
Hamish
  • 81.3k
  • 19
  • 196
  • 298
Source Link
BLC
  • 2.3k
  • 25
  • 29

Access control in swift 4

While upgrading to Swift4 from Swift3, I got some issues related to access control.

Here is the sample code. Which was there in Swift3, working fine in past times -

open class MyClass { private let value: Int static var defaultValue: Int { return 10 } public init(value: Int = MyClass.defaultValue) { self.value = value } } 

To make the code run in Swift4, I have to change access control for defaultValue to public. Here is the Swift4, compiling version

open class MyClass { private let value: Int static public var defaultValue: Int { return 10 } public init(value: Int = MyClass.defaultValue) { self.value = value } } 

While I was wondering what is going on, I tried to remove open access control for MyClass, it allowed me to remove access identifier for defaultValue. Even can put it to private.

class MyClass { private let value: Int private static var defaultValue: Int { return 10 } public init(value: Int = MyClass.defaultValue) { self.value = value } } 

I understand all the access identifiers, but I am not able to understand this behaviour. Especially the first case where xcode forced me to change access control of defaultValue to public.

Please help.