简体   繁体   English

要浮动的TextField文本(Objective-C)

[英]TextField text to float (Objective-C)

如何将TextField内的文本转换为Objective-C中的float?

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. 编辑:从技术上讲,最合适的方法是使用NSNumberFormatter ,因为如果它无法正确解析字符串,则返回nil NSNumber ,而floatValue只返回0.0表示格式错误的字符串。 But floatValue is usually adequate for most purposes. floatValue通常适用于大多数用途。

If it's an NSTextField you call floatValue on the actual object, not on what's returned from text . 如果它是NSTextField ,则在实际对象上调用floatValue ,而不是从text返回的text NSTextField does not have a text method. NSTextField没有text方法。

float myFloat = [myTextField floatValue];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM