views:

65

answers:

3

Hi!

I have a small problem with iphone sdk. I have 4 values into my xib, 4 UITextField where I insert hostname, description, name and PORT. Port is numeric number (NSNUMBER). I must convert text (UITextField) into an NSNUMBER object but I don't know how. I try to do: NSNumber *temp = [[NSNumber alloc] initWithString:portTextField.text]; serverObj.port = temp;

but App crash. I haven't problems with NSString (description ecc.)

Thank you!

+3  A: 

There is no method for NSNumber objects called initWithString: (that's why your app crashes). You need to use NSNumberFormatter's method numberFromString:

RupertP
+1  A: 

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 ] ];
Macmade
thanks to everyone!
Crowley
+3  A: 
NSNumberFormatter * f = [[NSNumberFormatter alloc] init];
[f setNumberStyle:NSNumberFormatterDecimalStyle];
NSNumber * myNumber = [f numberFromString:textField.text];
[f release];

And that should get the job done.

Matt S.
it works, thx!! :)
Crowley
Just be sure to hit the checkmark button to mark it as the answer :)
Matt S.