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.)

Answers


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:


Need Your Help

Pymongo / MongoDB: create index or ensure index?

python mongodb pymongo

I don't understand the difference between create_index and ensure_index in pymongo. On the MongoDB indexes page, it says

ajax calling C# method

c# jquery asp.net ajax

I want to call C# method using jquery ajax, the method doesn't return any thing instead it directly modifies the data in aspx page. My call is working perfectly alright but I am unable to make any