Assign textView's value to int, IOS
I have a textView (tF1) that will be supplied with a numeric value by the user. I need to assign that to number to the int (n).
This is what I came up with:
#import "blabla.h"
int n;
....
[tF1 resignFirstResponder];
n = (int)[tF1 text];
NSLog(@"n is, %d",n);
My problem is that the int value comes out to be a vary large number, like 82296768, for a textview value of 4.
Any and all help wo开发者_开发知识库uld be greatly appreciated! :)
SOLVED
changed line, n = (int)[tF1 text];
to, n = [[tF1 text] initValue];
NSString
has a method intValue
that you should be using:
n = [[tF1 text] intValue];
From the documentation for NSString, the other numeric values you can retrieve are:
– doubleValue
– floatValue
– intValue
– integerValue
– longLongValue
– boolValue
To invert this process, that is to add a numeric value as a string, you can use the NSString
method -stringWithFormat:
, e.g.
[tF1 setText:[NSString stringWithFormat:@"%d", someIntValue]];
You should be using an NSNumberFormatter
to get the numeric value. Take a look at the method numberFromString:
in the documentation.
精彩评论