2

I have a library that is written in Objective-C and using .m and .h files.

Is it possible for me to write an extension method for that class and get access to a private property that is only defined in the .m file and not defined in the .h file?

I have tried with the .valueForKey but I only get

payload_data_0
payload_data_1
payload_data_2
instance_type

which all are of type Builtin.RawPointer.

Jeggy
  • 1,474
  • 1
  • 19
  • 35
  • 2
    Private is private for a reason. You should not be attempting to access such a property. It's breaks encapsulation. – rmaddy May 07 '17 at 17:00
  • I know, but for me to write this extension method I'm gonna need access to it? so you don't think it's possible for me to access with some reflection way maybe? (if that exists in swift) – Jeggy May 07 '17 at 17:02

2 Answers2

1

No. The only way for you to access it is to define it publicly in the header.

Oskar
  • 3,625
  • 2
  • 29
  • 37
1

No, technically you can't. The documentation is very clear about this:

Extensions can add new computed properties, but they cannot add stored properties, or add property observers to existing properties.

If you need this for testing purpose, consider moving the private property declaration to a separate header file, like MyClass+Test.h , and have it added to the bridging header. But guess what, this makes those properties public anyways. Not a recommended solution.

Mehul Parmar
  • 3,599
  • 3
  • 26
  • 42