开发者

Write only property in Objective-C

开发者 https://www.devze.com 2023-01-26 12:33 出处:网络
I a开发者_StackOverflowm 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

I a开发者_StackOverflowm 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.


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.


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号