简体   繁体   中英

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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