-1

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.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Herr Derb
  • 4,977
  • 5
  • 34
  • 62
  • 1
    There're no correlation between NSRange or CGSize, both serve different purposes. I am wondering, what do you intend to achieve here? – ldindu Nov 15 '14 at 22:46
  • 3
    Define your own `struct` using `NSInteger`. You can make it `start` and `length` or `start` and `end` depending on your needs. – rmaddy Nov 15 '14 at 22:47
  • @ldindu I added some information to my question, to better understand what I'm trying to achieve. – Herr Derb Nov 15 '14 at 23:33
  • @rmaddy This is a neat idea, but I think it's not practical in this case, as I need to choose from a give list(see updated question) – Herr Derb Nov 15 '14 at 23:34

1 Answers1

0

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.

ldindu
  • 4,270
  • 2
  • 20
  • 24
  • I guess this is the best way to do it. I would have loved it to make this with one user defined runtime attribut, but this doesn't seem to be possible. Thanks – Herr Derb Nov 16 '14 at 00:31