[swift-users] Property access for subclasses only

James Dempsey dempsey at mac.com
Fri Mar 17 13:54:05 CDT 2017


I have a class in a framework that is expected to be subclassed in modules outside of the framework. I’d like to do the following:

1. I want some properties of that class to be accessible by subclasses outside of the framework.
2. I don’t want these properties of that class to be accessible by any piece of code outside of the framework because I don’t want dependencies on these properties to propagate beyond the subclass.

Am I correct in thinking that there is no way to achieve both #1 and #2 in Swift?

The only way I know to achieve #1 is to make the property public or open. And that makes the property public to everyone which makes #2 impossible.

It seems like the best I can do is put a big comment on each property that says something like:
// NOTE: FOR SUBCLASSERS ONLY! DO NOT USE EXCEPT IN A SUBCLASS!

But that does not seem like a very Swift-y way of doing things.

It’s quite possible I’m misunderstanding something - is there a way to do what I am trying to do, or a better way to achieve this?

Thanks,

James

—————————
James Dempsey
dempsey at mac.com




More information about the swift-users mailing list