5

I get a value from a TextField and I need to check if there's a number in it. Whether it's a float or an integer doesn't really matter, but definitely a "Number"

How can I catch this?

This is what I'm doing so far - even if the obj is 123 (actually a number), the condition is false, so I can't get into the if. I already tried NSValue and Data, but with the same results.

id obj = self.textArea.text;

if ([obj isKindOfClass:[NSNumber class]]) {
    self.weight = [NSNumber numberWithFloat:([self.textArea.text floatValue])];
lnafziger
  • 25,760
  • 8
  • 60
  • 101
Helen Wood
  • 1,872
  • 2
  • 26
  • 41
  • 4
    You may need to check this question: http://stackoverflow.com/questions/6091414/finding-out-whether-a-string-is-numeric-or-not – Hejazi Jun 28 '13 at 01:35
  • 1
    You should not check obj on NSNumber class. It will as NSString class. But second line is ok. – stosha Jun 28 '13 at 01:35
  • possible duplicate of [How to convert an NSString into an NSNumber](http://stackoverflow.com/questions/1448804/how-to-convert-an-nsstring-into-an-nsnumber) – Monolo Jun 28 '13 at 07:02

3 Answers3

6

The text of a text field will ALWAYS be a NSString since that is how the class was designed.

Your task, then, is to convert it into a NSNumber. The best way to do this is to use a number formatter like this:

NSNumberFormatter *formatter = [NSNumberFormatter new];
self.weight = [formatter numberFromString:self.textArea.text];
if (!self.weight) {
    // No valid number was found.
}
lnafziger
  • 25,760
  • 8
  • 60
  • 101
0

You cant use floatValue since it will return 0.0 if there is either a 0 or no number at all. You cant use NSNumberFormatter as suggested by Inafziger if there are non number chars in the textfield.

Use:

NSCharacterSet *numberSet = [NSCharacterSet characterSetWithCharactersInString:@"0123456789"];
NSRange numberRange = [self.textArea.text rangeOfCharacterFromSet:numberSet];
if (numberRange.location == NSNotFound) {
    NSLog(@"no");
} else {
    NSLog(@"yay");
}
Danilo
  • 3,257
  • 2
  • 19
  • 24
  • I actually prefer to use the number formatter to make sure that we only end up with valid numbers from the text field. If there are non-numeric characters, what should the number really be anyway?? Also, `float` is notoriously imprecise and usually won't give the exact entered value if you need to display it to the user at a later time. If you want a scalar type (even though he is storing it as an `NSNumber`) then at least use `double`. – lnafziger Jun 28 '13 at 02:01
  • "I need to check if there's a number in it". As far as i understood it, there could be a number inside a string or not. If thats correct, you cant use the number formatter as it will return nil in that case. – Danilo Jun 28 '13 at 02:08
  • Well, I interpreted that as checking to see if there is a number *in the text field*, and since he is storing it as an `NSNumber` in `self.weight` it makes sense. At any rate, if there is a mix of numeric and non-numeric characters, then they should be parsed in some way to make sure that they are valid anyway... – lnafziger Jun 28 '13 at 02:10
  • 1
    Sure. If your interpretation is correct, i would use a number formatter too. – Danilo Jun 28 '13 at 02:11
0

Use a regular expression to test the value, then once you know it is a number you can do whatever else you need to.

NSError *error = NULL;
NSString *pattern = @"^[0-9]*\.?[0-9]+$"
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:pattern options:NSRegularExpressionCaseInsensitive erro:&error];
NSUInteger matches = [regex numberOfMatchesInString:self.textarea.text options:0 range:NSMakeRange(0, [self.textarea.text length])];
if (matches > 0)
{
     // do whatever you need to do now that you know it's a number
}
mjk
  • 541
  • 2
  • 4