Search code examples
xcodensrangensuinteger

Xcode:(user defined runtime attribute)Range with minus value. Alternative?


I made a custom UITextField with an additional user defined runtime attribute validRange, which I can set in the storyboard view. I use this property to check in the EndEditing method to validate the new set text. I works all fine, till I had to set a valid range from {-100,100} As NSRange uses NSUInteger, there are no minus values possible.

What is the best way to still make this happen? Would it be acceptable if I use CGSize instead of NSRange?

Updated Content Xcode only gives me the following choice of data types for the user defined runtime attributes:

enter image description here

This means I cannot define a new struct to create a CustomRange with NSInteger. As Point,Size are both {NSInteger,NSInteger} data types, I thought about using them. But this would be certainly a misuse, so I am wondering if someone knows a better solution, as misusing Point or Size to get this to work.

As another workaround I could user String, which I manually would split up in a method of the custom UITextField, but then there is no type safety.


Solution

  • Then I would suggest you to define two NSNumber properties with suitable names to represent NSRange value instead of abusing CGSize as using CGSize confuses other readers/programmers as we there is a saying, we code for others not for ourselves.

    And there is a NSNumber class method as follows

    + (NSNumber *)numberWithInteger:(NSInteger)value
    

    which allow you to wrap signed integer value as you intend.