Getting the contents of a text field as a number
I have a textfield in my app, whre the user can enter any number (I have set to Number pad) I want to store this as an integer in a variable I am writing
int numOfY开发者_如何学JAVAears = [numOfYearsFld text];
But for some reason it is taking it incorrectly e.g. if user enters 10, it taakes as 10214475
Am I doing something wrong ?
intValue
returns an int
.
integerValue
returns an NSInteger
.
NSInteger numOfYears = [[numOfYearsFld text] integerValue];
[numOfYearsFld text] will be a NSString*. Try:
int numOfYears = [[numOfYearsFld text] intValue];
Try:
int numOfYears = [[numOfYearsFid text] intValue];
or, preferably
NSInteger numOfYears = [[numOfYearsFid text] integerValue];
intValue
returns an int
and integerValue
returns an NSInteger
.
精彩评论