Write only property in Objective-C

江枫思渺然 提交于 2019-12-05 08:41:29

问题


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.


回答1:


You can do something like:

@interface MyClass {
@private
int _var;
}

- (void)setVar:(int)newVar;
@end

@implementation MyClass
- (void)setVar:(int)newVar {
_var = newVar;
}
@end

Now you can access the variable var like a property that is write-only:

@implementation SomeOtherClass
...
MyClass c = [[MyClass alloc] init];
c.var = 3;
...
@end

And if you try to read the fake property the compiler will warning you.




回答2:


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.




回答3:


Is there any reason this needs to be a property? I'd just declare the setter as a method and use the normal [foo setVar:bar] syntax.



来源:https://stackoverflow.com/questions/4266197/write-only-property-in-objective-c

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!