Yeah, you'd generally make each attribute a @property
and provide a custom setter which either rejects or corrects invalid values:
.h file:
@interface Employee : NSObject
@property (assign, nonatomic, readwrite) int personality;
// others omitted
@end
.m file:
@implementation Employee
- (void)setPersonality:(int)personality {
if (personality < 1)
personality = 1;
else if (personality > 99)
personality = 99;
// _personality is an auto-generated backing instance variable
_personality = personality;
}
// - (int)personality { ... } will be auto-generated
If every one of those attributes has a min/max value then create an static
function to restrict the value:
static int restrict(int minValue, int maxValue, int value) {
if (value < minValue)
value = minValue;
else if (value > maxValue)
value = maxValue;
return value;
}
...
- (void)setPersonality:(int)personality {
// _personality is an auto-generated backing instance variable
_personality = restrict(1, 99, personality);
}
Note: that you must not assign _personality
(and friends) directly; you must use self.personality = 34;
or employee.personality = 34;
to invoke the setter.