开发者

TextField text to float (Objective-C)

开发者 https://www.devze.com 2022-12-15 08:03 出处:网络
How do I convert the text ins开发者_运维技巧ide of a TextField to a float in Objective-C?float myFloat = [[myTextField text] floatValue];

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];

0

精彩评论

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