How do I convert the value of a UITextField to an NSNumber?
I have a small problem with iPhone SDK. I have 4 values in my xib, 4 UITextFields where I insert hostname, description, name and PORT. The Port is an NSNumber. I must convert text (UITextField) into NSNumber but I don't know how. I tried the following:
NSNumber *temp = [[NSNumber alloc] initWithString:portTextField.text]; serverObj.port = temp;
but my app crashes. I don't have problems with NSString (description ecc.)
NSNumberFormatter * f = [[NSNumberFormatter alloc] init]; [f setNumberStyle:NSNumberFormatterDecimalStyle]; NSNumber * myNumber = [f numberFromString:textField.text]; [f release];
And that should get the job done.
Just use the 'integerValue' method of NSString, to get a 'NSInteger'. Then you can create a NSNumber object from it.
serverObj.port = [ NSNumber numberWithInteger: [ portTextField.text integerValue ] ];
There is no method for NSNumber objects called initWithString: (that's why your app crashes). You need to use NSNumberFormatter's method numberFromString: