开发者

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 conve开发者_StackOverflowrt 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:

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜