How do I convert the text ins开发者_运维技巧ide of a TextField to a float in Objective-C?
float myFloat = [[myTextField text] floatValue];
Edit: technically the most proper way to do this would be to use an NSNumberFormatter
, since that will return a nil
NSNumber
if it can't parse the string properly, whereas floatValue
will just return 0.0 for a malformed string. But floatValue
is usually adequate for most purposes.
If it's an NSTextField
you call floatValue
on the actual object, not on what's returned from text
. NSTextField
does not have a text
method.
float myFloat = [myTextField floatValue];
精彩评论