In C# I could do this:
class RangeValidator<T> {
public T MinValue { get; set; }
public T MaxValue { get; set; }
}
Where T could be any primitive type; int, float, double… or any “object”-type; String, DateTime etc.
If in Obj-C, I did it like this:
@interface RangeValidator {
id minValue;
id maxValue;
}
@property ...
It would work for let’s say a NSNumber or NSString, but if I assigned a NSInteger to minValue I’d get something like a
warning: initialization makes pointer from integer without a cast
// Since an id is a pointer to an object, not an integer. Correct?
The obvious solution here is maybe to use a NSNumber instead. I was just curious if there are any other solutions to this kind of problem?
Thanks!
The correct way would be to use a
NSNumberand initialize from the integer withThis way the class does not need generics, you could easily perform validation by checking if
minValueandmaxValuee.g. understandsintValue(or some comparison selector maybe, depending on what you want to do).