Very simple with the private modifier, followed by the Limit keyword set:
class Day{ privatevar0 func showRawValue(){ print("raw is \(rawValue)") } func forwardRawValue(){ 1 }}
However, if the above class definition is in playground, you will actually find that if you remove the following (set) you can access it externally:
let day = Day()day.rawValue+=1//that‘s OK!day.forwardRawValue()day.showRawValue()
This is because the private modifier in Swift does not have the same meaning as in traditional object-oriented objects, and in swift, private only restricts visibility in the same file. So to think that it really works, you have to be able to understand it in an external file:
class ViewController: UIViewController { func test(){ let day = Day() //day.rawValue = 99 Error!!! day.showRawValue() day.forwardRawValue() day.showRawValue() }}
That ' s ok!;]
How to create a read-only internally writable property in the Swift class