开发者

How to mark that an non-object property has not been set?

For example, I have properties like this:

@property (nonatomic, assign) CGFloat startValue;
@property (nonatomic, assign) BOOL running;

It's a cascading system similar to CSS which inherits values if they're not explicitely changed. However, there is a class describing the whole state. It has to have properties for just about everything. One can change some values down in the hirarchy to override inherited ones, just by modifying some of the properties. It's a chain of instances.

The problem: With this design it's impossible to determine if a property is overriding a value from the "master object" or not.

I need a safe way to mark if an property has been explicitly set to something or not. For the CGFloat I could say if the value is < -99999 it's not set explicitly and we simply use the value of the master object. But how about the BOOL?

I could also forget about properties and switch to dictiona开发者_如何转开发ries. They allow to iterate over keys so I can easily apply the changes only. But they're a pain in the butt to use all over the place. That's why I want a simple class where I can just set properties. Way easier to use, no ugly and heavy wrapping of values in NSNumber objects.

I am sure there are intelligent ways to do this.


There are a few ways to accomplish that:

  • For the BOOL case, you could simply use an int instead and define that 1 is equivalent to YES, 0 is equivalent to NO, and -1 is equivalent to unset.

  • Alternatively, you could use pointers to those types. The pointer being NULL would indicate that a value hasn’t been set; the pointer being different from NULL would indicate that a value has been set and the value is available at the address stored in the pointer. You’d have to manage memory in the C way (malloc(), free()).

  • You could box your numeric types into instances of NSNumber. Similarly to the previous case, if the property is nil then it hasn’t been set; otherwise, unbox the corresponding value.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜