I’d like to declare a public immutable property:
@interface Foo
@property(strong, readonly) NSSet *items;
@end
…backed with a mutable type in the implementation file:
@interface Foo (/* private interface*/)
@property(strong) NSMutableSet *items;
@end
@implementation
@synthesize items;
@end
What I want is a mutable collection in the implementation that gets cast into an immutable one when accessed from the outside. (I don’t care that the caller can cast the instance back to NSMutableSet
and break the encapsulation. I live in a quiet, decent town where such things don’t happen.)
Right now my compiler treats the property as NSSet
inside the implementation. I know there are many ways to get it working, for example with custom getters, but is there a way to do it simply with declared properties?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…