copenndthagen
copenndthagen

Reputation: 50722

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 numOfYears = [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 ?

Upvotes: 0

Views: 153

Answers (3)

MHC
MHC

Reputation: 6405

Try:

int numOfYears = [[numOfYearsFid text] intValue];

or, preferably

NSInteger numOfYears = [[numOfYearsFid text] integerValue];

intValue returns an int and integerValue returns an NSInteger.

Upvotes: 0

Espresso
Espresso

Reputation: 4752

intValue returns an int.

integerValue returns an NSInteger.

NSInteger numOfYears = [[numOfYearsFld text] integerValue];

Upvotes: 1

fsaint
fsaint

Reputation: 8759

[numOfYearsFld text] will be a NSString*. Try:

int numOfYears = [[numOfYearsFld text] intValue];

Upvotes: 1

Related Questions