Write only property in Objective-C

KingofHeaven picture KingofHeaven · Nov 24, 2010 · Viewed 9.4k times · Source

I am stuck with objective-c properties. What I need is to assign a write-only property for a variable, exactly the opposite of readonly, i.e the variable can have setMethod, but it should not have getMethod. I don't know how to do it. answers with some code snippets are appreciated.

Answer

Gabriele Petronella picture Gabriele Petronella · Sep 5, 2013

Another valid approach is to declare the normal property and make the getter unavailable

@interface MyClass
@property NSString * var;
- (NSString *)var UNAVAILABLE_ATTRIBUTE;
@end

This will raise a compile-time error in case someone tries to access the getter.


The main advantage of this approach is that you actually have a real propertyTM and not a surrogate faked by a instance variable and a setter, which implies that:

  • you'll get auto-completion when using dot-notation in Xcode
  • all the runtime functions such as class_getProperty and class_copyPropertyList will work as expected.

That said, the getter implementation is synthesized anyway and it could be invoked using something like

NSString * string = [myObj performSelector:@selector(var)];

or using NSInvocation (in case of a non-id property)

If you are paranoid and you really want to prevent the client to invoke the getter, you can explicitly throw an exception by providing your own implementation.

@implementation MyClass
- (int)var {
    [NSException raise:NSInternalInconsistencyException
                format:@"property is write-only"];
}
@end

Anyway, if someone seriously wants to tamper with your instance variable, they can use the runtime to access it, so the last precaution is probably useless given the use case. A compile-time error is what you are really looking for.