I need to set an NSInteger to the number in a text field. And I've googled it and could not find any answer. Does anybody know how to do this
Asked
Active
Viewed 370 times
2 Answers
2
NSInteger x = [textField.text integerValue];

hsoi
- 2,764
- 3
- 19
- 20
-
Thanks. Do you know how you would do that with a subtraction method like this NSInteger givedback = givedback - [self.textField.text integerValue]; When I run the app and enter a number in comes up with a crazy number – Jun 03 '13 at 18:56
-
Hard to say from here. Would have to see it more in context. My recommendation when debugging is to make simple expressions instead of compound, because it's easier to examine in the debugger. For instance, assign the integerValue of textField to "x", then do the math of 'givedback - x'. Then you can see if perhaps x is wrong and the problem is in extraction and conversion there. You might even try assigning textField.text to a local NSString, seeing the true string value, etc.. Just something for you to debug. – hsoi Jun 03 '13 at 19:15
-
@Programer Does XCode generate warning about `NSInteger givedback = givedback - [self.textField.text integerValue];` code? Because you're creating variable and then using this variable in it's own initialization. – derpoliuk Jun 05 '13 at 08:29
-
@StasDerpoliuk Yes It does. It works when I try to minus it with an number – Jun 08 '13 at 17:44
-
@Programer if this was the correct answer, could you come back and accept it? – hsoi Oct 11 '13 at 18:43
0
John C. Daub answer is perfect. I would add this also:
NSInteger x = [[NSNumber numberWithInteger:[field.text integerValue]] integerValue];
The advantage of NSNumber
is that if you want to pass this integer into dicitonary, NSNumber helps.
On the other hand a call to integerValue
would be sufficient.

NeverHopeless
- 11,077
- 4
- 35
- 56
-
1
-
did you mean `NSNumber x = [NSNumber numberWithInteger:[field.text integerValue]];` ? – derpoliuk Jun 05 '13 at 08:02
-
@StasDerpoliuk, left hand side will be NSInteger. so it should be: `NSInteger x = [NSNumber numberWithInteger:[field.text integerValue]];` for this case atleast. Other you can take NSNumber on the left side as well. – NeverHopeless Jun 05 '13 at 08:15
-
@NeverHopeless `NSNumber` is an object and `NSInteger` is a simple type Only way `NSInteger` could be on the left side: `NSInteger x = [[NSNumber numberWithInteger:[field.text integerValue]] integerValue];` wich looks kinda overhead – derpoliuk Jun 05 '13 at 08:25
-
It is an overhead if you are going to use it for NSInteger on the left side but it is useful when you pass it in dictionary. There you need type `id`. – NeverHopeless Jun 05 '13 at 08:30
-
To clarify my earlier comment, the original code was taking the -integerValue of the text and creating the NSNumber using +numberWithInt:. It's a mixing of "int" and "NSInteger" which, strictly speaking, aren't the same thing. – hsoi Jun 05 '13 at 10:18
-
-
@StasDerpoliuk, with an overhead, you can. My post was missing last call of `integerValue` – NeverHopeless Jun 10 '13 at 07:20