0

I am new to iOS. I need to convert textfield integer value into string. I created textfield name as value1 and string as str1.

str1 = [NSString stringWithFormat:@"%d", value1];
rmaddy
  • 314,917
  • 42
  • 532
  • 579
A.sonu
  • 159
  • 10
  • Possible duplicate of [retrieving integer value from the UITextField into a NSInteger variable](http://stackoverflow.com/questions/886925/retrieving-integer-value-from-the-uitextfield-into-a-nsinteger-variable) – David V May 10 '16 at 13:23

1 Answers1

2

By default, UITextField returns a NSString value.

So you can get any value like this :

NSString *str1 = value1.text;

As simple as that, but if you want to convert it to Int then,

int strInt = str1.intValue;
NSInteger strInteger = str1.integerValue;

If want to converts to a Double value,

double strDouble = str1.doubleValue;
CGFloat strCGFloat = str1.doubleValue;

And yes, if you don't need to perform anything on str1 then you shouldn't need to create an instance, you can directly convert it like this.

int strInt = value1.text.intValue;
NSInteger strInteger = value1.text.intValue;
Hemang
  • 26,840
  • 19
  • 119
  • 186