Centurion
Centurion

Reputation: 14304

How to define typedef for a range of float numbers?

I want a property that will only accept float values between 0 and 1. I could do that in a standard way by defining float_t or double_t type for the property, but doubting, are there more elegant ways for that?

Upvotes: 0

Views: 93

Answers (2)

Sulthan
Sulthan

Reputation: 130102

Do it the standard way - define a double/float property and check for validity in the setter - example:

@property (nonatomic, assign, readwrite) float property;

@synthesize property = _property;

- (void)setProperty:(float)property {
   NSAssert(property >= 0.0f && property <= 1.0f, @"Invalid value passed to property setter.").   

   _property = property;
}

Upvotes: 3

CRD
CRD

Reputation: 53000

(Objective-)C doesn't support subrange types. For that you need Ada (or a few others). The best you can do is implement checks in your setters.

Upvotes: 1

Related Questions