2

This post shows that Objective-C do not have "real" private methods or properties, which means that even if you do not expose them in the header file, you can still access them at runtime. Is this the case in Swift properties and methods marked with private?

0

1 Answer 1

2

While the compiler prevents you from accessing private properties directly, you still have read-only access to their values via Swift's nascent introspection. Consider a structure with two private variables:

// FileOne.swift struct Secret { private var password = "Password" private var secretNumber = 42 } 

In a different file, we create an instance. The compiler won't let us access secretNumber or password directly, but we can use reflect to get what we want:

// FileTwo.swift var a = Secret() var b = reflect(a) for i in 0..<b.count { println("\(b[i].0): \(b[i].1.value)") } // password: Password // secretNumber: 42 

Private methods are unreachable this way, for now.

Sign up to request clarification or add additional context in comments.

Comments